gpt4 book ai didi

javascript - 爱彼迎/javascript : What does this mean: "These methods may be shadowed by properties ..."?

转载 作者:行者123 更新时间:2023-11-30 07:53:41 24 4
gpt4 key购买 nike

我在github airnb/javascript 尝试理解下面这句话

https://github.com/airbnb/javascript#objects--prototype-builtins

Why? These methods may be shadowed by properties on the object in question

在这种情况下,“阴影”是什么意思?

为了更容易引用这里的完整部分:

3.7 Do not call Object.prototype methods directly, such as hasOwnProperty, propertyIsEnumerable, and isPrototypeOf.

Why? These methods may be shadowed by properties on the object in question - consider { hasOwnProperty: false } - or, the object may be a null object (Object.create(null)).

// bad
console.log(object.hasOwnProperty(key));

// good
console.log(Object.prototype.hasOwnProperty.call(object,key));

// best
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
/* or */
import has from 'has';
// ...
console.log(has.call(object, key));

最佳答案

当你创建一个对象时

const anObject = {};

它几乎总是在原型(prototype)链中有Object。它允许创建的对象访问 Object 中定义的函数,例如 hasOwnProperty

Shadowed 表示创建对象中定义的方法或属性与原型(prototype)链中的函数或属性同名。

阴影示例:

const obj = {};
obj.hasOwnProperty // => ƒ hasOwnProperty() { [native code] }

obj.hasOwnProperty = 'don\'t do this!';
obj.hasOwnProperty // => "don't do this!"

关于javascript - 爱彼迎/javascript : What does this mean: "These methods may be shadowed by properties ..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46419263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com