gpt4 book ai didi

javascript - javascript 中的裸对象是 ECMAScript 标准的一部分吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:54 24 4
gpt4 key购买 nike

我遇到了 this article如果您的键始终是字符串,则建议使用“裸对象”来满足您的 HashMap 需求。

裸对象是使用 null 作为原型(prototype)值创建的对象,例如使用 Object.create(null)。使用对象文字表示法(即 {})不会创建裸对象,因为它们将 Object.prototype 设置为原型(prototype)。

文章指出,裸对象的优点在于您可以将它们用作散列映射,而不必担心像 toString 这样的内置键在使用同名键时可能会导致错误。

这种行为是 ES5 和/或 ES6 标准的一部分吗?也就是说,如果我在我的代码中使用裸对象作为字符串键散列映射,我可以依赖我的代码以我期望的方式运行吗?这里有什么注意事项吗?

最佳答案

首先,ECMA-Script 2015 及以上版本有像Map 这样的集合。 .也就是说,在较新的 JavaScript 实现中,您不再需要使用对象模拟字典/ HashMap /哈希表。

The article points out that the great thing about bare objects is that you can use them as hashmaps without having to worry about builtin keys like toString potentially causing bugs when using a key with the same name.

这篇文章忽略了您不需要担心文字对象中的 toString,因为有很好支持的函数可以在不遍历原型(prototype)链的情况下获取自己对象的属性。

例如,假设我声明了一个文字对象如下:var obj = { text: "Matias"};

常规 for..in 循环会迭代 Object.prototype 属性,但是 Object.keys 会迭代自己的对象属性 仅:

Object.keys(obj).forEach(propertyName => {
var someOwnProperty = obj[propertyName ];
});

此外,常规的 for..in 可以使用 Object.prototype.hasOwnProperty 用作 Object.keys:

for(var propertyName in obj) {
if(obj.hasOwnProperty(propertyName)) {
// True if property is declared on obj and not in some
// level of the prototype chain
}
}

此处更新:@bergi 在某些方面是正确的。如果 obj 将声明一个自己的属性 hasOwnProperty,上面的 for..in 将不起作用因为 obj.hasOwnProperty 不会是 Object.prototype.hasOwnProperty不再

假设您有以下会产生上述问题的对象:

var obj = {
hasOwnProperty: "hey! I'm not Object.prototype.hasOwnProperty anymore!"
};

hasOwnProperty 会隐藏 Object.prototype.hasOwnProperty .

可以使用 Object.prototype.hasOwnProperty 规避上述问题直接与 Function.prototype.call :

for(var propertyName in obj) {
if(Object.prototype.hasOwnProperty.call(obj, propertyName)) {
// True if property is declared on obj and not in some
// level of the prototype chain
}
}

或者您可以将 Object.prototype.hasOwnProperty 存储在一个变量中,以简化 if 语句设置函数获取后的 thisFunction.prototype.bind 调用:

var hasOwnProperty = Object.prototype.hasOwnProperty.bind(obj);

for(var propertyName in obj) {
if(hasOwnProperty(propertyName)) {
// True if property is declared on obj and not in some
// level of the prototype chain
}
}

创建裸对象并将它们用作原型(prototype)的副作用

虽然您可以使用 Object.create(null) 创建裸对象,但是当给定的裸对象 是其他一些对象:

var bareObject = Object.create(null, {
text: { value: "hello world" }
});

var secondObject = Object.create(bareObject);
secondObject.text2 = "bye!";

for(var property in secondObject) {
// WAIT, secondObject prototype is a bare object!
// And I can't call secondObject.hasOwnProperty to check
// if the enumerated property is declared in the own object...
}

如果不是这种情况并且给定对象只是一个裸对象,您可以使用in 运算符:

if("someProperty" in bareObject) {

}

否则,您需要使用 Function.prototype.callFunction.prototype.bind 调用 Object.prototype.hasOwnProperty如上所述,在我的回答中。

无论如何,正如我在回答开头所说的那样,如果您使用的是 ES2015 及更高版本,并且您使用的是像 BabelJS 这样的转译器,您可以使用新的 JavaScript 标准集合类型,而不是使用对象模拟字典。

Is this behavior part of the ES5 and/or ES6 standard? That is, if I use bare objects as string key hashmaps in my code, can I rely on my code behaving in the way that I would expect? Are there any caveats to here?

Object.create 是在 ECMA-Script 5 中引入的。它在几乎所有现代 Web 浏览器和 NodeJS 中的行为都符合您的预期。

关于javascript - javascript 中的裸对象是 ECMAScript 标准的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911978/

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