admin管理员组文章数量:1314488
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
?
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
?
- You aren't using a prototype. – SLaks Commented Jun 23, 2011 at 16:56
- No, I am changing the prototype of a function. :) This is not about the library indeed. – user142019 Commented Jun 23, 2011 at 16:56
- 1 Um, couldn't you have figure it out yourself testing it? :) – epascarello Commented Jun 23, 2011 at 17:11
4 Answers
Reset to default 5Yes, a prototype change affects all instances of the item whose prototype you modified.
See example of a simple prototype modification: http://jsfiddle/JAAulde/56Wdw/1/
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
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.
The following code would print something:
var ajs = new _JS();
_JS.prototype.do = function () {console.log('something');}
ajs.do();
本文标签: javascriptIf a prototype is changedwill this affect current instancesStack Overflow
版权声明:本文标题:javascript - If a prototype is changed, will this affect current instances? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741962301a2407350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论