What does enumerable mean?
I was directed to MDN's for..in page when it said, "for..in Iterates over the enumerable properties of an object."
Then I went to the Enumerability and ownership of properties page where it said "Enumerable properties are those which can be iterated by a for..in loop."
The dictionary defines enumerable as countable, but I can't really visualize what that means. Could i get an example of something being enumerable?
Well, whether a property is considered enumerable or not is based on its own [[Enumerable]] attribute. You can view this as part of the property's descriptor:
var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar');
console.log(descriptor.enumerable); // true
console.log(descriptor.value); //
console.log(descriptor);
// { value: 1, writable: true, enumerable: true, configurable: true }
> var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar');
undefined
>
undefined
> console.log(descriptor.enumerable); // true
true
undefined
> console.log(descriptor.value); //
1
undefined
>
undefined
> console.log(descriptor);
{ value: 1, writable: true, enumerable: true, configurable: true }
undefined
> // { value: 1, writable: true, enumerable: true, configurable: true }
undefined
>
A for..in loop then iterates through the object's property names.
var foo = { bar: 1, baz: 2};
for (var prop in foo)
console.log(prop); // outputs 'bar' and 'baz'
> var foo = { bar: 1, baz: 2};
undefined
>
undefined
> for (var prop in foo)
... console.log(prop); // outputs 'bar' and 'baz'
bar
baz
undefined
But, it only evaluates its statement -- console.log(prop); in this case -- for those properties whose [[Enumerable]] attribute is true.
This condition is in place because objects actually have many more properties, especially those from inheritance:
> console.log(Object.getOwnPropertyNames(Object.prototype));
[ 'constructor',
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'__defineGetter__',
'__lookupGetter__',
'__defineSetter__',
'__lookupSetter__' ]
undefined
> // ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty",
"isPrototypeOf", "propertyIsEnumerable", /* etc. */]
console.log(Object.getOwnPropertyNames(Object.prototype));
// ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", /* etc. */]
Each of these properties still exists on the object:
console.log('constructor' in foo); // true
console.log('toString' in foo); // true
// etc.
But, they're skipped (or "not counted") by the for..in loop because they're non-enumerable.
var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'constructor'); console.log(descriptor.enumerable); // false
What does enumerable mean?的更多相关文章
- 【EF学习笔记12】----------解释查询和本地查询 区分 Enumerable 和 Queryable
简单介绍:Enumerable 和 Queryable 他们都是静态类,位于命名控件 System.Linq下,分别为IEnumerable<T>和IQueryable<T>提 ...
- C#高级功能(二)LINQ 和Enumerable类
介绍LINQ之前先介绍一下枚举器 Iterator:枚举器如果你正在创建一个表现和行为都类似于集合的类,允许类的用户使用foreach语句对集合中的成员进行枚举将会是很方便的.我们将以创建一个简单化的 ...
- C#的枚举数(Enumerator)和可枚举类型(Enumerable)
数组可以被foreach语句遍历数组中的元素,原因是数组可以按需提供一个叫做枚举数(enumerator)的对象.枚举数可以依次返回请求的数组的元素. 对于有枚举数的类型而言,必须有一个方法来获取它们 ...
- 【C#】详解使用Enumerable.Distinct方法去重
Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...
- LINQ Enumerable
System.Linq.Enumerable类,提供了数十种称为扩展方法的共享方法,帮助您操作所有实现IEnumerable(of T)接口的类中的数据.由于Enumerable类的扩展方法可以处理许 ...
- LINQ Enumerable 续 II
Enumerable.TakeWhile和Enumerable.SkpWhile Enumerable.TakeWhile和Enumerable.SkpWhile将通过判断条件,来获取和跳过序列. T ...
- LINQ Enumerable 续
筛选序列 Enumerable.Distinct 对于复杂的对象列表,运行时引擎如何才能通过比较确定两个对象是否重复?对于复杂对象,必须提供一个比较器,即实现IEqualityComparer(Of ...
- js对象中什么是可枚举性(enumerable)?
说到枚举,可能很多人都会想到枚举类型,但在javascript对象中有一个属性为可枚举性,他是什么呢? 概念 可枚举性(enumerable)用来控制所描述的属性,是否将被包括在for...in循环之 ...
- 整理一下 System.Linq.Enumerable 类中的那些比较少用的方法
Linq 虽然用得多,但是里面有一些方法比较少用,因此整理一下.Enumerable 类的所有方法可以在 MSDN 上查阅到:https://msdn.microsoft.com/zh-cn/libr ...
随机推荐
- python 文件查找 glob
glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:"*", "?&quo ...
- CSS 背景颜色
颜色背景 CSS中背景颜色由background-color决定,这里的背景颜色会渲染padding和content,不会渲染border和margin部分. 在css3中可以通过background ...
- call()和apply()的区别
var a = function(a,b){ console.log(a+b); }, b = { c:5, d:3 }; a.call(b,1,2); a.apply(b,[1,2]); a.cal ...
- 【弱省胡策】Round #6 String 解题报告
感觉这个题好神啊. 首先我们只管 $a = b$ 的情况,那么我们自然就可以把这个串对 $a$ 取模,然后用 KMP 求出能弄出几个其他的 B 串. 具体就是把串先倍长,然后倒过来,然后求 $Next ...
- codeforces 388C Fox and Card Game
刚刚看到这个题感觉是博弈题: 不过有感觉不像,应该是个贪心: 于是就想贪心策略: 举了一个例子: 3 3 1 2 3 4 3 4 1 2 5 4 1 2 5 8 如果他们两个每次都拿对自己最有利的那个 ...
- android 自定义标题栏 titleBar自定义
在value文件夹下添加style.xml <?xml version="1.0" encoding="utf-8"?> <resources ...
- 运行edX Devstack
前一篇文章,我们安装完成了edX Devstack,本文将介绍edX Devstack的常用命令 6.1. 连接到 Devstack Virtual Machine 为了连接到Devstack vir ...
- [topcoder]FoxAndChess
http://community.topcoder.com/stat?c=problem_statement&pm=12725&rd=15702 这题比较简单.首先所有的LR的顺序要一 ...
- asp.net上传图片(简单)
FileUpload: protected void UploadButton_Click(object sender, EventArgs e) { : Convert.ToInt64(Reques ...
- [HDU 1430] 魔板
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...