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

Posted by Devon on Stack Overflow See other posts from Stack Overflow or by Devon
Published on 2008-09-17T15:48:13Z Indexed on 2010/03/21 4:11 UTC
Read the original article Hit count: 313

Filed under:
|

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

For example:

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();

Is there a better way to do this? Is there a cleaner way to create the class above with only one statement instead of two?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery