for ... in循环(JavaScript)中继承的非可枚举属性 [英] Inherited non-enumerable properties in for...in loop (JavaScript)

查看:227
本文介绍了for ... in循环(JavaScript)中继承的非可枚举属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象 p ,它带有一个可枚举的 x

I have an object p, with an enumerable propery x.

var p = Object.create(Object.prototype, {
  x: {
    value: "frog",
    enumerable: true
  }
});

我创建一个继承自 p 的对象,并覆盖property x ,带有不可枚举的属性

I create an object, that inherits from p, and overrides property x, with a non-enumerable property

var o = Object.create(p, {
  x: {
    value: "bird",
    enumerable: false
  }
});

您认为,属性 x 会枚举在for ...在 o 循环中?

What do you think, will the property x be enumerated in a for...in loop of o?

for (var n in o) {
  console.log(n);
}

好的,在Chrome和IE9中会列举出来(这是我认为的,相当奇怪);在FF中,它不会被枚举。

Well, in Chrome and IE9 it will be enumerated (which is, I think, quite wierd); in FF, it won't be enumerated.

哪一个是正确的行为?

这是一个完整的例子: http://jsfiddle.net/hnvsM/3/

Here is a complete example: http://jsfiddle.net/hnvsM/3/

推荐答案

这是V8中的一个已知错误。有一段时间有关于此的错误报告。

This is a known bug in V8. There's been a bug report on this for quite a while.

问题705:非可枚举属性无法将继承的可枚举属性映射到for-in

这篇关于for ... in循环(JavaScript)中继承的非可枚举属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆