Reputation: 14318
Please run the following code snippet 1 and see what is happening in JS console:
My questions are regarding the last line in the snippet:
F.prototype.method;
changed?Fcustom.prototype.method
in order to not change F.prototype.method
? Note: I am using jQuery and underscore to extend the function.
1 Test code snippet:
var F = function () {};
F.prototype.method = function () {
// some code
}
F.prototype.method; // it shows "some code"
Fcustom = $.extend(true, F, {});
_.extend(Fcustom.prototype, {
method: function () {
// other code
}
});
Fcustom.prototype.method; // it shows "other code"
F.prototype.method; // it shows "other code" instead of "some code" Why?
Upvotes: 5
Views: 112
Reputation: 23054
var obj = { myMethod : function() {
//some code
}
};
var newObj = $.extend(true, {}, obj);
newObj.myMethod = function (){
//new method
};
newObj.myMethod(); //should call the new method
While,
obj.myMethod(); //still calls the old "//some code"
DEMO:
Upvotes: 3