简体   繁体   English

jQuery和面向对象的JavaScript - howto?

[英]jQuery and object-oriented JavaScript - howto?

I've read this and this (thanks google) But it doesn't help enough. 我已经读过这个这个 (感谢谷歌)但它没有足够的帮助。 I'd like to know, if, straight out of the box, without any plugin addons, it's possible to do something like it's possible with prototype, for example: 我想知道,如果,开箱即用,没有任何插件插件,可以做一些像原型一样的事情,例如:

MyClass = Class.create(Table,
{
  cookieName: 'w_myclass',
  prefix: 'myclass',
    ...blabla...

  // function
  initStr: function()
  {
    ...blabla...
  },

  // another function
  getIndice: function(param)
  {
    ...blabla...
    return 0;
  }

});

Any idea/suggestions are welcome. 欢迎任何想法/建议。

JQuery never had the purpose of being a class framework. JQuery从未有过成为类框架的目的。 It's about page manipulation and tools (like AJAX). 它是关于页面操作和工具(如AJAX)。 You can pound a nail with a fork, but why not use a hammer? 你可以用叉子敲钉子,但为什么不用锤子?

Using native JavaScript to create a class-based inheritance system is asking for trouble unless you're a highly skilled JavaScript programmer. 除非你是一个技术娴熟的JavaScript程序员,否则使用本机JavaScript创建基于类的继承系统会遇到麻烦。 Douglas Crockford will tell you it's possible , but he has a deep understanding if the intricacies of closure etc etc. Also, using native inheritance features becomes unsustainable very quickly if your system grows large. 道格拉斯·克罗克福德会告诉你这是可能的 ,但他对封闭等的复杂性有深刻的理解。此外,如果你的系统变大,使用本机继承功能会变得不可持续。

I highly recommend James Coglan's JS.Class framework. 我强烈推荐James Coglan的JS.Class框架。 The class definitions will look almost identical to your example. 类定义看起来与您的示例几乎相同。 It's not native JS but it works fine with JQuery. 它不是本机JS,但它适用于JQuery。

If you want a near object oriented solution using javascript with jquery you can define an object in javascript that will set up your event controllers. 如果你想使用带有jquery的javascript的近似面向对象的解决方案,你可以在javascript中定义一个将设置你的事件控制器的对象。

The second half of this post http://www.codescream.com/?p=18 covers that. 这篇文章的后半部分http://www.codescream.com/?p=18涵盖了这一点。 but i'll write here a resume on how to make an object in javascript that you can use in a near object oriented structure. 但我会在这里写一篇关于如何在javascript中创建一个可以在近物体导向结构中使用的对象的简历。

It would look something like this: 它看起来像这样:

function myObject(constructorParam1, constructorParam2, anotherOne, blabla){

  var text = "";
  // this event will be set everyTime you run myObject
  $(".foo").click(function(){
    text = $(this).text(); // copies the text inside the elements ".foo" to a local variable
    doSomething();
  });

  function aPrivateFunction1(){


  }

  function aPrivateFunction2(){

  }

  function internalAdd(a,b){
    return a+b;
  }

  var size = 1; // privateVaribale
  var name = blabla;


  if(name===undefined){
     name="No name";
  }

  aPrivateFunction1(); // run "aPrivateFunction1()

  // you can consider all code above as being part of the constructor.
  // The variables declared above are private, and the functions are private as well

  // bellow are public functions  that you can access in an OOP manner
  return {
    getSize: function(){
        return size;
    },

    setSize: function(newSize){
        size = newSize;
    },

    getName: function(){
        return name;
    },

    setName: function(newName){ 
        name = newname;
    },

    addAndTurnPositive: function(n1,n2){
        var val = internalAdd(n1,n2);
        if(val<0){
            return val*-1;
        }
        return val;
    }
  }
}

// then you can run it like
var anInstance = myObject("aaa",1234,"asdak",123);
anInstance.setSize(1234);
var t = anInstance.addAndTurnPositive(-5,2);

In a word, no. 总之,没有。 jQuery doesn't offer any class and inheritance functionality. jQuery不提供任何类和继承功能。 You'd need to include another library, such as klass (although there is a jQuery plugin which ties it in more closely with jQuery) 你需要包含另一个库,比如klass (虽然有一个jQuery插件 ,它与jQuery更紧密地联系在一起)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM