Given the following code (jQuery pattern, I suppose):
function child(){
return new child.methods.init();
}
child.methods = child.prototype = {
init: function(){
return this;
},
test: function(){
console.log('test');
return this;
}
}
//this code doesn't work
child.methods.prototype = {
test1: function(){
console.log('test1');
return this;
}
}
child.methods.init.prototype = child.methods;
//Using child chaining api. test() will work, but test1() will not.
child().test().test1();
I need to inherit child from other object (object literal is given to simplify). How can I inherit child function pattern from object?
Child.method is iteself child.prototype. Why you are creating a new prototype attribute inside child.prototype/child.methods. You can acheive chaining as per your requirement.