gpt4 book ai didi

javascript - x) 中的 ("__proto__"可以返回 false 吗?

转载 作者:行者123 更新时间:2023-11-29 20:49:14 26 4
gpt4 key购买 nike

我正在寻找使该函数返回 true 的参数值

function mustBeTrue(param) {
return param && !("__proto__" in param);
}

我尝试解决这个问题如下:我以为我可以从任何对象中删除属性 __proto__ 并将其传递给 param 但它不能被删除。

var obj = {foo: 'bar'};

delete obj.foo; //testing delete
console.log('foo' in obj); //false
delete obj.__proto__
console.log('__proto__' in obj); //true Why ??

最佳答案

除了@Pac0 的回答:

实际上,'__proto__' in {} 在某些浏览器(IE < 11)中返回 true

目前,所有现代浏览器都实现了 __proto__ 属性,它不能被删除,但它被标记为已弃用,所以事情可能很快就会改变(或者可能不会像现在的标准那样)。

来自 MDN (强调我的):

The use of __proto__ is controversial, and has been discouraged. It was never originally included in the EcmaScript language spec, but modern browsers decided to implement it anyway. Only recently, the __proto__ property has been standardized in the ECMAScript 2015 language specification for web browsers to ensure compatibility, so will be supported into the future. It is deprecated in favor of Object.getPrototypeOf/Reflect.getPrototypeOf and Object.setPrototypeOf/Reflect.setPrototypeOf

关于javascript - x) 中的 ("__proto__"可以返回 false 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52759567/

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