为什么使用Object.prototype.hasOwnProperty.call(myObj,prop)而不是myObj.hasOwnProperty(prop)? [英] Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

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

问题描述

如果我理解正确,Javascript中的每个对象都继承自Object原型,这意味着Javascript中的每个对象都可以通过其原型链访问hasOwnProperty函数。

If I understand correctly, each and every object in Javascript inherits from the Object prototype, which means that each and every object in Javascript has access to the hasOwnProperty function through its prototype chain.

在阅读require.js的源代码时,我偶然发现了这个函数:

While reading require.js' source code, I stumbled upon this function:

function hasProp(obj, prop) {
    return hasOwn.call(obj, prop);
}

hasOwn 是一个引用 Object.prototype.hasOwnProperty 。编写此函数有什么实际区别吗

hasOwn is a reference to Object.prototype.hasOwnProperty. Is there any practical difference to writing this function as

function hasProp(obj, prop) {
    return obj.hasOwnProperty(prop);
}

既然我们在这,我们为什么要定义这个功能呢?这只是关于(轻微)性能提升的属性访问的快捷方式和本地缓存的问题,还是我错过了任何可能在没有此方法的对象上使用hasOwnProperty的情况?

And since we are at it, why do we define this function at all? Is it just a question of shortcuts and local caching of property access for (slight) performance gains, or am I missing any cases where hasOwnProperty might be used on objects which don't have this method?

推荐答案


我的例子之间是否存在实际差异?

Is there any practical difference [between my examples]?

用户可能有一个用 Object.create(null)创建的JavaScript对象,它将具有 null [[Prototype]] chain,因此不会有 hasOwnProperty()。因为这个原因,使用你的第二个表格将无法工作。

The user may have a JavaScript object created with Object.create(null), which will have a null [[Prototype]] chain, and therefore won't have hasOwnProperty() available on it. Using your second form would fail to work for this reason.

它也是对 Object.prototype.hasOwnProperty()(还有更短)。

It's also a safer reference to Object.prototype.hasOwnProperty() (and also shorter).

你可以想象有人可能已经做过......

You can imagine someone may have done...

var someObject = {
    hasOwnProperty: function(lol) {
        return true;
    }
};

哪个会产生 hasProp(someObject)失败,如果你的第二个例子被实现(它会直接在对象上找到该方法并调用它,而不是被委托给 Object.prototype.hasOwnProperty )。

Which would make a hasProp(someObject) fail had it been implemented like your second example (it would find that method directly on the object and invoke that, instead of being delegated to Object.prototype.hasOwnProperty).

但不太可能有人会覆盖 Object.prototype.hasOwnProperty 引用。

But it's less likely someone will have overridden the Object.prototype.hasOwnProperty reference.


既然我们在这,我们为什么要定义这个功能呢?

And since we are at it, why do we define this function at all?

见上文。


是吗
只是一个快捷方式和本地缓存属性访问的问题,
(轻微)性能提升......

Is it just a question of shortcuts and local caching of property access for (slight) performance gains...

理论上它可能会更快,因为不必遵循 [[Prototype]] 链,但我怀疑这是可以忽略不计,并且实现原因的原因。

It may make it quicker in theory, as the [[Prototype]] chain doesn't have to be followed, but I suspect this to be negligible and not the reason the implementation is why it is.


...或者我错过任何
hasOwnProperty 的情况可能会用在没有这种方法的对象上?

... or am I missing any cases where hasOwnProperty might be used on objects which don't have this method?

hasOwnProperty()存在于 Object.prototype 上,但可以覆盖。每个本机JavaScript对象(但不保证主机对象都遵循此对象,参见RobG的深入解释 Object.prototype 作为 null 之前链上的最后一个对象(当然除了返回的对象> Object.create(null))。

hasOwnProperty() exists on Object.prototype, but can be overridden. Every native JavaScript object (but host objects are not guaranteed to follow this, see RobG's in-depth explanation) has Object.prototype as its last object on the chain before null (except of course for the object returned by Object.create(null)).

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

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