首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有更好的方法用jQuery创建面向对象的类?

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

Stack Overflow用户
提问于 2008-09-17 15:48:13
回答 6查看 93.7K关注 0票数 71

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

例如:

代码语言:javascript
复制
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();

有没有更好的方法来做这件事?有没有一种更干净的方法来创建上面的类,只用一条语句而不是两条语句?

EN

回答 6

Stack Overflow用户

发布于 2008-09-17 16:01:17

我很喜欢John Resig的Simple JavaScript Inheritance

代码语言:javascript
复制
var MyWidget = Class.extend({
  init: function(widget_name){
    this.widget_name = widget_name;
  },

  doSomething: function() {
    alert('my name is ' + this.widget_name);
  }
});

注:上面演示的"Class“对象不包含在jQuery本身中-它是jQuery先生自己的25行代码片段,在上面的文章中提供。

票数 53
EN

Stack Overflow用户

发布于 2012-12-23 09:30:28

为什么不在jQuery之前使用JavaScript本身provides...long的简单OOP呢?

代码语言:javascript
复制
var myClass = function(){};
myClass.prototype = {
    some_property: null,
    some_other_property: 0,

    doSomething: function(msg) {
        this.some_property = msg;
        alert(this.some_property);
    }
};

然后,您只需创建该类的一个实例:

代码语言:javascript
复制
var myClassObject = new myClass();
myClassObject.doSomething("Hello Worlds");

很简单!

票数 24
EN

Stack Overflow用户

发布于 2008-09-17 22:44:51

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

下面是让Class.extend()在jQuery中工作的基本函数(由John Resig从Simple JavaScript Inheritance复制):

代码语言:javascript
复制
// 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中执行以下代码:

代码语言:javascript
复制
var MyWidget = Class.extend({
  init: function(widget_name){
    this.widget_name = widget_name;
  },

  doSomething: function() {
    alert('my name is ' + this.widget_name);
  }
});

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

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/84716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档