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

前端 未结 6 472
温柔的废话
温柔的废话 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:23

    Why not just use the simple OOP that JavaScript itself provides...long before jQuery?

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

    Then you just create an instance of the class:

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

    Simple!

提交回复
热议问题