- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我遇到了 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
语句设置函数获取后的 this
用 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...
}
如果不是这种情况并且给定对象只是一个裸对象,您可以使用in
运算符:
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 - javascript 中的裸对象是 ECMAScript 标准的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911978/
我对 Lift Cookbook for AJAX Forms 感到困惑,我遇到了以下对象声明: object EchoForm extends { 这很令人困惑,所以我尝试了一下,它编译得很好。我的
我正尝试按照指示在裸 CSS 模态窗口内实现视频 here到目前为止效果很好。我遇到的唯一问题是视频在模态窗口打开之前自动启动(我必须保留视频“自动启动”选项)。模态窗口关闭时视频也不会停止。 是否有
同样,裸 char16_t 是有符号的还是无符号的?是否定义了实现? 最佳答案 来自标准(C++11 前草案 n3290,或 C++11 后草案 n3337)§3.9.1 基本类型: Types ch
我使用 C# 编程已经...有一段时间了。我最近编写了一个例程,我突然想到这是我第一次(我记得)我故意使用裸代码块(即没有前面的控制流语句)。代码看起来像这样: //... var output =
我最近开始使用 nativescript,但我遇到了一个似乎无法解决的问题。 我想要完成的是只打开一个基本的 WebView 并设置一个外部 url 来加载,例如 stackoverflow.com。
我通过以下方式创建了一个共享仓库: git init --bare my_project.git 在某个时候,另一个用户用他的更改更新了这个 repo(使用 git push )。 我如何检查共享存储
我刚刚浏览完 Google 搜索结果,其中包含所有关于 git 无法克隆空存储库是多么愚蠢的电子邮件咆哮。一些善良的灵魂甚至提交了一个补丁。在升级 git 之前,克隆空的、裸 git 存储库的最简单、
我喜欢编写一些 wordpress 模板,并搜索了一些可用的裸模板,我可以从这些模板开始。 因为我是 wordpress 的新手,你能推荐最好的裸模板作为我可以使用的基础吗? 干杯 最佳答案 在我看来
我正试图找到一个最新的答案,以使用 Godaddy 和 Heroku SSL 设置裸域。我的目标是让所有页面都登陆安全页面 https://www.example.com https://www.ex
这个问题在这里已经有了答案: What's the -practical- difference between a Bare and non-Bare repository? (11 个答案) 关
我运行一个 git 服务器,它只包含裸远程。比如说,我有一个名为 DIG.git 的裸 Remote ,我通常使用它进行克隆: git clone 55.66.77.88:git/DIG.git 我想
是否有一个用于裸 git repo 目录的 GUI(任何地方都没有工作树)我可以: 检查日志 查看任何提交的整个工作树结构 关于我为什么需要这个: 我的 git 是这样初始化的: git --git-
我有一台服务器托管我的几个项目的“裸”GIT 存储库。 我想知道如何将 GIT 移动到另一台服务器? 最佳答案 只需将父目录复制到新机器,并让 check out 的副本使用 git remote s
我有一个域 example.com 和 www.example.com。我正在为 DNS 使用 Heroku 和 Zerigo。现在我有一个从根域到 www.example.com 的转发从我的 Ho
我的服务器已经在 TCP 端口 80 和 443 上运行 IIS。我想通过 Internet 为我的所有团队成员提供一个集中的“推/pull ”Git 存储库。 所以我应该使用 HTTP 或 HTTP
裸露的 SUM 和包裹在 CALCULATE 中的 sum 有什么区别? Measure1 = SUM( tab[col1]) ) Measure2 = CALCULATE ( SUM( tab[co
我是一名优秀的程序员,十分优秀!