代码之家  ›  专栏  ›  技术社区  ›  Devon

有没有更好的方法用jquery创建面向对象的类?

  •  70
  • Devon  · 技术社区  · 17 年前

    我使用jquery extend 函数来扩展类原型。

    例如:

    MyWidget = function(name_var) {
      this.init(name_var);
    }
    
    $.extend(MyWidget.prototype, {
       // object variables
       widget_name: '',
    
       init: function(widget_name) {
         // do initialization here
         this.widget_name = widget_name;
       },
    
       doSomething: function() {
         // an example object method
         alert('my name is '+this.widget_name);
       }
    });
    
    
    // example of using the class built above
    var widget1 = new MyWidget('widget one');
    widget1.doSomething();
    

    有更好的办法吗?有没有一种更简洁的方法来创建上面只有一个语句而不是两个语句的类?

    6 回复  |  直到 7 年前
        1
  •  52
  •   Jonny Buchanan    17 年前

    我很喜欢约翰·瑞斯格的 Simple JavaScript Inheritance .

    var MyWidget = Class.extend({
      init: function(widget_name){
        this.widget_name = widget_name;
      },
    
      doSomething: function() {
        alert('my name is ' + this.widget_name);
      }
    });
    

    注意:上面演示的“类”对象并没有包含在jquery本身中——它是来自jquery先生本人的25行代码片段,在上面的文章中提供。

        2
  •  25
  •   J0HN    12 年前

    为什么不在jquery之前使用javascript本身提供的简单oop呢?

    var myClass = function(){};
    myClass.prototype = {
        some_property: null,
        some_other_property: 0,
    
        doSomething: function(msg) {
            this.some_property = msg;
            alert(this.some_property);
        }
    };
    

    然后创建类的实例:

    var myClassObject = new myClass();
    myClassObject.doSomething("Hello Worlds");
    

    简单的!

        3
  •  15
  •   Shiladitya    7 年前

    总结一下我到目前为止学到的东西:

    下面是使class.extend()在jquery中工作的基函数(从 Simple JavaScript Inheritance 作者:John Resig)

    // Inspired by base2 and Prototype
    (function(){
      var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
    
      // The base Class implementation (does nothing)
      this.Class = function(){};
    
      // Create a new Class that inherits from this class
      Class.extend = function(prop) {
        var _super = this.prototype;
    
        // Instantiate a base class (but only create the instance,
        // don't run the init constructor)
        initializing = true;
        var prototype = new this();
        initializing = false;
    
        // Copy the properties over onto the new prototype
        for (var name in prop) {
          // Check if we're overwriting an existing function
          prototype[name] = typeof prop[name] == "function" &&
            typeof _super[name] == "function" && fnTest.test(prop[name]) ?
            (function(name, fn){
              return function() {
                var tmp = this._super;
    
                // Add a new ._super() method that is the same method
                // but on the super-class
                this._super = _super[name];
    
                // The method only need to be bound temporarily, so we
                // remove it when we're done executing
                var ret = fn.apply(this, arguments);       
                this._super = tmp;
    
                return ret;
              };
            })(name, prop[name]) :
            prop[name];
        }
    
        // The dummy class constructor
        function Class() {
          // All construction is actually done in the init method
          if ( !initializing && this.init )
            this.init.apply(this, arguments);
        }
    
        // Populate our constructed prototype object
        Class.prototype = prototype;
    
        // Enforce the constructor to be what we expect
        Class.constructor = Class;
    
        // And make this class extendable
        Class.extend = arguments.callee;
    
        return Class;
      };
    })();
    

    一旦运行并执行了此代码,则 insin's answer 可能的:

    var MyWidget = Class.extend({
      init: function(widget_name){
        this.widget_name = widget_name;
      },
    
      doSomething: function() {
        alert('my name is ' + this.widget_name);
      }
    });
    

    这是一个好的,干净的解决方案。但我想看看是否有人有一个不需要向jquery添加任何内容的解决方案。

        4
  •  3
  •   noah    17 年前

    jquery没有提供这个功能。但是 Prototype 通过,通过 Class.create .

        5
  •  0
  •   Sam Arul Raj T    13 年前

    我发现这个网站是一个令人印象深刻的javascript面向对象编程的网站。 Here

        6
  •  0
  •   Shiladitya    7 年前

    这个插件已经死了很久了,但是如果其他人搜索jquery creating类,请检查这个插件: http://plugins.jquery.com/project/HJS