Is there a better way to create an object-oriented class with jQuery?

前端 未结 6 475
温柔的废话
温柔的废话 2020-12-04 05:50

I use the jQuery extend function to extend a class prototype.

For example:

MyWidget = function(name_var) {
  this.init(name_var);
}

$.extend(MyWidge         


        
6条回答
  •  天涯浪人
    2020-12-04 06:33

    I quite like John Resig's 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);
      }
    });
    

    NB: The "Class" object demonstrated above isn't included in jQuery itself - it's a 25 line snippet from Mr. jQuery himself, provided in the article above.

提交回复
热议问题