为什么是Object.defineProperty()而不是this.defineProperty()(对象)? [英] Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?

查看:89
本文介绍了为什么是Object.defineProperty()而不是this.defineProperty()(对象)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个JavaScript项目,只是想知道为什么对象实例不会继承 defineProperty()和其他方法,而不是必须调用超类(superobject?)对象方法。

I'm working on a JavaScript project, and was just wondering why an object instance doesn't inherit the defineProperty() and other methods, rather than having to call the superclass (superobject?) Object method.

我看过 MDN docs ,实际上有非标准属性方法。

I've looked at the MDN docs, and there are in fact "non-standard" property methods.

但这些已被弃用。为什么移动到对象方法?

But those are deprecated. Why would the move be to the Object methods?

在我看来像 instance.defineProperty(...)优于 Object.defineProperty(instance,...)。我也会对其他一些Object方法说同样的话。

It seems to me that something like instance.defineProperty(...) is better than Object.defineProperty(instance, ...). I would say the same about some of the other Object methods as well.

推荐答案

这是为了避免碰撞 - 一般来说,问题是没有具有您期望值的属性的对象。

JS中的对象通常用作键值映射,键可以是任意字符串 - 例如 __defineGetter __ hasOwnProperty 或更不特别的东西。现在,当你想在一个未知对象上调用这样一个函数时 - 比如 hasOwnProperty 经常用在通用枚举函数中,其中任何JSON都可以传入 - 你永远无法确定是否有一个覆盖的属性(可能不是一个函数)或你想要的原始属性,或者对象是否继承了属性。要避免此问题(或此IE错误),您必须使用对象。 prototype.hasOwnProperty.call - 这很难看。

It's to avoid collisions - in general, issues with objects that do not have the property with the value that you expect.
Objects in JS are often used as key-value-maps, and the keys can be arbitrary strings - for example __defineGetter__, hasOwnProperty or something less special. Now when you want to invoke such a function on an unknown object - like hasOwnProperty is often used in generic enumeration functions, where any JSON might be passed in - you can never be sure whether you got a overwritten property (that might not even be a function) or the original which you want, or whether the object inherits the property at all. To avoid this issue (or also this IE bug), you'd have to use Object.prototype.hasOwnProperty.call - that is ugly.

所以,命名空间 Object 只是有用的,它是一个更清晰的API,它将反射方法与对象的应用程序接口分开。这也有助于优化(简化静态分析),并且更容易限制对沙箱中反射API的访问 - 至少是设计理念

So, namespacing all those functions on Object is only useful, it's a cleaner API that separates the reflection methods from the object's application interface. This also helps optimisation (simplifying static analysis) and makes it easier to restrict access to the reflection API in sandboxes - at least that was the design idea.

您可能很高兴在原型中有一个 defineProperty ,但您只能使用它使用已知对象时安全。如果您仍然需要它(如您所知,何时不使用),您可以使用

You might be happy to have a defineProperty around in the prototype, but you can only use it safely when working with known objects. If you still want it (as you know when to use and when not), you could use

Object.defineProperty(Object.prototype, "defineProperty", {
    writable: true,
    enumberable: false,
    value: function(prop, descr) {
        return Object.defineProperty(this, prop, descr); 
    }
});

这篇关于为什么是Object.defineProperty()而不是this.defineProperty()(对象)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆