原型更改后,Javascript 全局对象停止工作!
如果我遗漏了一些明显的东西,我不会,但是当我执行如下操作时:-
Object.prototype.inherit = function(fromClass) {
fromClass.apply(this, Array.prototype.slice.call(arguments, 1));
for(var key in fromClass.prototype){
if(typeof fromClass.prototype[key] == 'function')
this.prototype[key] = fromClass.prototype[key];
}
};
然后我无法再使用任何对象。我总是得不到定义。类似地,当我尝试将某些方法“注入”到 Array.prototype 中时,数组将停止工作!我使用的是火狐4.0.1。
我错过了什么吗?
I don't if I am missing something obvious, but when I do something like below:-
Object.prototype.inherit = function(fromClass) {
fromClass.apply(this, Array.prototype.slice.call(arguments, 1));
for(var key in fromClass.prototype){
if(typeof fromClass.prototype[key] == 'function')
this.prototype[key] = fromClass.prototype[key];
}
};
Then I can no longer work with any objects. I always get undefined. Similary when I try to 'inject' some method into Array.prototype
then arrays stop working! I am using Firefox 4.0.1.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请,请,请,请不要扩展 OBJECT.PROTOTYPE。
谢谢。
现在开始回答你的问题,你不能再使用对象是什么意思?您将打破 for-in 循环,但除此之外,一切都应该仍然有效。
PLEASE, PLEASE, PLEASE, PLEASE DO NOT EXTEND OBJECT.PROTOTYPE.
Thank you.
Now getting on to your question, what do you mean you can no longer work with objects? You're going to break for-in loops, but short of that everything should still work.