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 ...
随机推荐
- javascript高级编程笔记02(基本概念)
ParseInt()函数: 由于Number函数在转换字符串时比较复杂而且不合理,我们常常转换字符串都用parseInt函数, Parseint函数规则: 忽略字符串前面的空格,直到找到第一个非空格字 ...
- 分别用js和jq实现百度全选反选效果
js实现过程 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- oct(x) 将一个数字转化为8进制
>>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...
- treeview 点击时选中节点
private void tv_WebList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { Point clickPo ...
- Java 8 vs. Scala(二):Stream vs. Collection
[编者按]在之前文章中,我们介绍了 Java 8和Scala的Lambda表达式对比.在本文,将进行 Hussachai Puripunpinyo Java 和 Scala 对比三部曲的第二部分,主要 ...
- Python 之 使用 PIL 库做图像处理
http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.html Python 之 使用 PIL 库做图像处理 1. 简介. 图像 ...
- c# 捕捉键盘按键
//esc退出窗体 protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg , System.Windo ...
- php邮件发送 phpmailer
首先要安装phpmailer开源项目. 将class.phpmailer.php转移到php文件夹下, 编写代码: <?php require("class.phpmailer.php ...
- [topcoder]FoxAndChess
http://community.topcoder.com/stat?c=problem_statement&pm=12725&rd=15702 这题比较简单.首先所有的LR的顺序要一 ...
- lib 合并
方法1: 命令如下:lib.exe lib1.lib lib2.lib ... /out:hw.lib 另一种合并的方法: 思路:lib.exe 可以把lib文件里的obj文件取出来,然后把这些obj ...