javascript中的裸对象是ECMAScript标准的一部分吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript中的裸对象是ECMAScript标准的一部分吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3514字,纯文字阅读大概需要6分钟。
内容图文
我遇到过this article,如果您的密钥总是字符串,建议使用’裸对象’来满足您的hashmap需求.
裸对象是使用null作为原型值创建的对象,例如使用Object.create(null).使用对象文字符号(即{})不会创建裸对象,因为它们将Object.prototype设置为原型.
文章指出裸机对象的优点是你可以将它们用作哈希映射,而不必担心内置键,如toString,当使用同名的键时可能会导致错误.
这种行为是ES5和/或ES6标准的一部分吗?也就是说,如果我在代码中使用裸对象作为字符串键哈希映射,我可以依赖我的代码以我期望的方式运行吗?这里有什么警告吗?
解决方法:
首先,ECMA-Script 2015及更高版本的集合类似于Map.也就是说,在较新的JavaScript实现中,您不再需要使用对象模拟字典/ hashmaps / hashtables.
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,因为有很好的支持函数来获取自己的对象的属性而不需要遍历原型链.
例如,假设我已经声明了一个文字对象,如下所示: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语句设置,一旦使用Function.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
}
}
创建裸对象并将其用作原型的副作用
虽然您可以使用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...
}
如果不是这种情况并且给定对象只是一个裸对象,则可以在运算符中使用:
if("someProperty" in bareObject) {
}
否则,您需要使用Function.prototype.call或Function.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中的裸对象是ECMAScript标准的一部分吗?全部内容,希望文章能够帮你解决javascript中的裸对象是ECMAScript标准的一部分吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。