问题描述
Axel Rauschmayer的 Speaking Javascript:深入的程序员指南中提到了以下功能:
The following function is mentioned in Speaking Javascript: An In-Depth Guide for Programmers by Axel Rauschmayer:
function getDefiningObject(obj, propKey) {
obj = Object(obj); // make sure it’s an object
while (obj && !{}.hasOwnProperty.call(obj, propKey)) {
obj = Object.getPrototypeOf(obj);
// obj is null if we have reached the end
}
return obj;
}
正如作者所说,它的目的是迭代对象的属性链 obj
[并返回]具有自己的属性且键为 propKey
的第一个对象,或者 null
如果没有这样的对象。
Its purpose, as the author puts it, is "to [iterate] over the property chain of an object obj
[and return] the first object that has an own property with the key propKey
, or null
if there is no such object".
我理解这里的整体推理,但我不知道理解为什么 {}。hasOwnProperty.call(obj,propKey)
正在完成而不仅仅是 obj.hasOwnProperty(propKey)
。任何想法?
I understand the overall reasoning here, but what I don't understand is why {}.hasOwnProperty.call(obj, propKey)
is being done rather than just obj.hasOwnProperty(propKey)
. Any ideas?
推荐答案
通常情况下,开发人员会在构建时使用调用
-in类型以确保它们获得方法的正确本机行为而不是某些被覆盖的行为。
这是一项我们真正不应该采取的保护措施使用,但因为对象在JavaScript中是如此具有可塑性,它保证我们得到我们想要的行为。
Often, developers will use call
on a built-in type to ensure that they are getting the correct native behavior of a method and not some overridden behavior.
It is a protective measure that we really shouldn't have to use, but because Objects are so malleable in JavaScript, it guarantees we get the behavior we desire.
想象一下,如果有人(有意或无意地)创建了这样的对象:
Imagine if someone (who, intentionally or accidentally) created an object like this:
function SuperObject(){
this.foo = function(){
// Something here
};
this.hasOwnProperty = 42;
}
然后,你来了(没看过对象的实现)并写道:
And then, you came along (without having seen the implementation of the object) and wrote:
var mySuperObject = new SuperObject();
console.log("Does 'mySuperObject' instance have its own 'foo' property? " +
mySuperObject.hasOwnProperty("foo"));
你会得到这个:
function SuperObject(){
this.foo = function(){
// Something here
};
this.hasOwnProperty = 42;
}
var mySuperObject = new SuperObject();
// Safe way:
console.log("Does 'mySuperObject' instance have its own 'foo' property? " +
{}.hasOwnProperty.call(mySuperObject, "foo"));
// Unsafe way (fails):
console.log("Does 'mySuperObject' instance have its own 'foo' property? " +
mySuperObject.hasOwnProperty("foo"));
所以,我们得到这样的代码:
So, instead we get code like this:
// This creates a new "fresh" (and unaltered) object with "object literal"
// syntax that you know doesn't override "hasOwnProperty"
{}.hasOwnProperty.call(obj, propKey)
// This performs the same task, but uses the native prototype object
// of the built-in Object type that we also know hasn't been altered:
Object.prototype.hasOwnProperty.call(obj, propKey)
它强制在na的原型链上查找 hasOwnProperty
tive 对象
,同时:
It forces the hasOwnProperty
to be looked up on the prototype chain of the native Object
, while:
obj.hasOwnProperty(propKey)
依赖于该对象的特定实例上可用且正确的属性( obj
)。
relies on that property being available and correct on a particular instance of an object (obj
).
其他受欢迎的例子是Array类型:
Other popular examples of this are with the Array type:
// These work the same as the Object examples above, but just
// do it for the Array type:
[].forEach.call(); // Array literal syntax
Array.prototype.forEach.call(); // Explicit array syntax
这篇关于为什么在这里一般调用hasOwnProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!