当在JavaScript对象上进行foreatch时,是否可能将未定义作为键?
我一直在进行代码审查,我看到的代码如下:当在JavaScript对象上进行foreatch时,是否可能将未定义作为键?,javascript,undefined,Javascript,Undefined,我一直在进行代码审查,我看到的代码如下: var a = { ... }; // in reality, an actual object filled with key-value pairs for (var key in a) { if (!angular.isUndefined(key)) { // does this ever fail? do.stuff(); } } 我的问题是,key是否可以被取消定义(或者根据angular.isUndefine
var a = { ... }; // in reality, an actual object filled with key-value pairs
for (var key in a) {
if (!angular.isUndefined(key)) { // does this ever fail?
do.stuff();
}
}
我的问题是,key
是否可以被取消定义(或者根据angular.isUndefined
计算为true
)。这似乎不太可能,因为当我尝试执行以下操作时:
var a = { undefined: 'hello' }
原来未定义的
实际上是一个字符串
我的问题是键
是否可以是未定义的
否。对象的所有键(属性名)都转换为字符串。
未定义的
不是字符串,但对象文字语法在将任何键标记插入对象之前将其转换为字符串。如果这是一个映射,答案将不同。这不是地图。费利克斯是100%正确的。