Reputation:
Say I have this:
var _JS = function() {
this.Constants = {
True: 1,
False: 0,
Nil: null,
Unknown: void(0),
};
};
var JS = new _JS();
If I change it afterwards (add methods, using _JS.prototype.etc
), can I call those methods on JS
?
Upvotes: 2
Views: 886
Reputation: 19560
Yes, a prototype change affects all instances of the item whose prototype you modified.
See example of a simple prototype modification: http://jsfiddle.net/JAAulde/56Wdw/1/
Upvotes: 5
Reputation: 1214
The following code would print something:
var ajs = new _JS();
_JS.prototype.do = function () {console.log('something');}
ajs.do();
Upvotes: 3
Reputation: 82028
Yes. Modifying a prototype modifies all instances.
A simple test:
var f = function(){}
var g = new f()
f.prototype.trace = function(){alert(1)}
g.trace(); // alerts 1
Upvotes: 5
Reputation: 338218
If a prototype is changed, will this affect current instances?
Yes. If you change the prototype that existing object instances share, it will change for all of them.
Upvotes: 3