var A=function(){
};
$.extend(A.prototype, {
init:function(){
alert(\'A init\');
}
});
var B=function(){
};
$.extend(B.prototype,A.prototype,{
I was looking for something similar. None of the answers given really appealed to me, so I finally had a crack at it myself...
http://jsfiddle.net/tn9upue0/1/
Example Classes
Class implementation
Example output
My name is unknown. I am an animal with 4 legs.
My name is Rover. I am an animal with 4 legs. I say "woof". I can sit, stay, and roll over.
My name is Mittens. I am an animal with 4 legs. I say "meow".
My name is unknown. I am an animal with 2 legs. I say "tweet".
Sample code
$.Animal = function (options) {
return {
options: options || {},
_getName: function () {
return this.options.name || 'unknown';
},
_getLegs: function () {
return 4;
},
describe: function () {
return 'My name is ' + this._getName() + '. I am an animal with ' + this._getLegs() + ' legs.';
}
}
};
$.Dog = function (options) {
var parent = $.Animal(options);
return $.extend({}, parent, {
describe: function () {
var s = $.proxy(parent.describe, this)() + ' I say "woof".';
if (this.options.tricks) {
s += ' I can ' + this.options.tricks + '.';
}
return s;
}
});
};
$.Cat = function (options) {
var parent = $.Animal(options);
return $.extend({}, parent, {
describe: function () {
return $.proxy(parent.describe, this)() + ' I say "meow".';
}
});
};
$.Bird = function (options) {
var parent = $.Animal(options);
return $.extend({}, parent, {
_getLegs: function () {
return 2;
},
describe: function () {
return $.proxy(parent.describe, this)() + ' I say "tweet".';
}
});
};
var animal = $.Animal(),
rover = $.Dog({name: 'Rover', tricks: 'sit, stay, and roll over'}),
mittens = $.Cat({name: 'Mittens'}),
bird = $.Bird();
$('#out').html(
animal.describe() + '
' +
rover.describe() + '
' +
mittens.describe() + '
' +
bird.describe()
);