扩展 Object.prototype JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6877005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extending Object.prototype JavaScript
提问by Lime
I am not asking if this is okay:
我不是问这是否可以:
Object.prototype.method = function(){};
This is deemed evilby pretty much everyone, considering it messes up for(var i in obj)
.
考虑到它搞砸了,几乎每个人都认为这是邪恶的for(var i in obj)
。
The Real Question
真正的问题
Ignoring
无视
- Incompetent browsers(browsers that don't support
Object.defineProperty
) - Potential for property collision or overriding
- 无能的浏览器(不支持的浏览器
Object.defineProperty
) - 财产冲突或覆盖的可能性
Assuming you have some incrediblyuseful method, is this considered wrong/unethical?
假设您有一些非常有用的方法,这是否被认为是错误的/不道德的?
Object.defineProperty(Object.prototype, 'methodOnSteriods',{
value: function(){ /* Makes breakfast, solves world peace, takes out trash */ },
writable: true,
configurable: true,
enumerable: false
});
If you believe the above is unethical, why would they even implement the feature in the first place?
如果您认为上述内容是不道德的,那么他们为什么要首先实现该功能?
采纳答案by Alex Wayne
I think it's fine if it works in your target environment.
我认为如果它适用于您的目标环境就可以了。
Also I think prototype extension paranoia is overblown. As long as you use hasOwnProperty()
like a good developer that it's all fine. Worst case, you overload that property elsewhere and lose the method. But that's your own fault if you do that.
我也认为原型扩展偏执被夸大了。只要你hasOwnProperty()
像一个好的开发者一样使用,一切都好。最坏的情况是,您在其他地方重载了该属性并丢失了该方法。但如果你这样做,那是你自己的错。
回答by hugomg
I'd say this is almostas evil as before. The biggest problem, still the same as before, is that Object.prototype is global. While your method might currently be solving world peace, it might have overwriten someone else's method (that was guaranteeing galactic peace) or may be overwritten in the future by some library you have no control over (therefore plunging the world into chaos again)
我会说这几乎和以前一样邪恶。最大的问题,仍然和以前一样,是Object.prototype 是 global。虽然您的方法目前可能正在解决世界和平,但它可能已经覆盖了其他人的方法(即保证银河和平),或者可能在未来被您无法控制的某个图书馆覆盖(因此使世界再次陷入混乱)
New versions of Javascript have lots of features related to properties, such as definig a property to be enumerable/not enumerable, having getters and setters... Object.defineProperty existis to give control over this.
新版本的 Javascript 有许多与属性相关的功能,例如将属性定义为可枚举/不可枚举,具有 getter 和 setter... Object.defineProperty 存在来控制这一点。
From Mozilla Docs:
来自Mozilla 文档:
This method allows precise addition to or modification of a property on an object. Normal property addition through assignment creates properties which show up during property enumeration (for...in loop), whose values may be changed, and which may be deleted. This method allows these extra details to be changed from their defaults.
此方法允许精确添加或修改对象上的属性。通过赋值的正常属性添加会创建在属性枚举(for...in 循环)期间显示的属性,其值可能会更改,也可能会被删除。此方法允许从默认值更改这些额外的详细信息。
This new function is basically required to support the new features and you are supposed to use it on your own stuff. Being able to modify Object.prototype is just a side effect of it also being a "normal" object and is just as evil as before.
这个新功能基本上是支持新功能所必需的,你应该在你自己的东西上使用它。能够修改 Object.prototype 只是它也是一个“正常”对象的副作用,并且和以前一样邪恶。
回答by sacabuche
Well in "JavaScript: the good parts", there is a similar function, i think that is very usefull to improve javascript base objects (like String, Date, etc..), but just for that.
好吧,在“JavaScript:好的部分”中,有一个类似的功能,我认为这对改进 javascript 基础对象(如字符串、日期等)非常有用,但仅此而已。
// Add a method conditionally. from "JavaScript: the good parts"
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
}
}
回答by PeregrineYankee
.hasOwnProperty()
will exclude iteration through inherited properties, which I personally find is often more annoying than helpful. It largely defeats the usefulness of Object.create()
—which is ironic since the same guy who convinced everyone to do .hasOwnProperty()
also promoted Object.create()
.
.hasOwnProperty()
将通过继承属性排除迭代,我个人认为这通常比帮助更烦人。它在很大程度上破坏了Object.create()
- 这具有讽刺意味,因为说服每个人这样做的同一个人.hasOwnProperty()
也提升了Object.create()
。
Object.prototype should not be extended, for the reasons listed here. If you really do want to extend it, then make the extensions non-iterable.
出于此处列出的原因,不应扩展 Object.prototype。如果你真的想扩展它,那么使扩展不可迭代。
I realize this flies in the face of all of the published best-practices, but we really should stop “mandating” .hasOwnProperty()
on object key iterations and embrace the usefulness of direct object-to-object inheritance.
我意识到这与所有已发布的最佳实践背道而驰,但我们真的应该停止“强制”.hasOwnProperty()
对象键迭代并接受直接对象到对象继承的用处。
回答by alexcres
The short answer is Yes, you should do it.
简短的回答是肯定的,你应该这样做。
Before doing it, there are several precautions need to take:
1. using hasOwnProperty
when iterating object, but this isn't really a precautions, when iterating object, I am already using hasOwnProperty
anyway.
2. check if the name
in Object.prototype.name
has existed, this is very safe to avoid name collision.
3. take advantage of Object.defineProperty()
, just add extra safeguard layer.
As you can see, it's not very complicated.
Now comes the advantages once you have taken care of the risks/disadvantages:
1. method chaining, this just makes code more readable, terse, and making coding more enjoyable. In turn makes you happier, and your life easier.
2. solves the browser compatibility issue, you are doing polyfill anyway.
P.S.:
Don't do it when working with a large team for sure.
在做之前,有几个预防措施需要采取:
1.hasOwnProperty
迭代对象时使用,但这并不是真正的预防措施,迭代对象时,hasOwnProperty
反正我已经在使用了。
2.检查name
inObject.prototype.name
是否存在,这样可以很安全的避免名字冲突。
3.利用Object.defineProperty()
,只需添加额外的保护层。
如您所见,这不是很复杂。
考虑到风险/缺点后,现在的优点是:
1. 方法链,这只会使代码更具可读性、简洁性,并使编码更有趣。反过来,让你更快乐,让你的生活更轻松。
2.解决了浏览器兼容性问题,反正你是在做polyfill。
PS:
在与大型团队合作时不要这样做。