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;
}
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".
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…