博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
What does enumerable mean?
阅读量:6231 次
发布时间:2019-06-21

本文共 2607 字,大约阅读时间需要 8 分钟。

I was directed to MDN's  when it said, "for..in Iterates over the enumerable properties of an object."

Then I went to the  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 . You can view this as part of the :

var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar');console.log(descriptor.enumerable); // trueconsole.log(descriptor.value);      // 1console.log(descriptor);// { value: 1, writable: true, enumerable: true, configurable: true }
> var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar');undefined>undefined> console.log(descriptor.enumerable); // truetrueundefined> console.log(descriptor.value);      // 11undefined>undefined> console.log(descriptor);{ value: 1, writable: true, enumerable: true, configurable: true }undefined> // { value: 1, writable: true, enumerable: true, configurable: true }undefined>

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'barbazundefined

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 , especially :

> 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 :

console.log('constructor' in foo); // trueconsole.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

 

转载于:https://www.cnblogs.com/ghgyj/p/4001245.html

你可能感兴趣的文章
63. Unique Paths II
查看>>
989-数组形式的整数加法
查看>>
Redis 源码分析之故障转移
查看>>
React as a UI Runtime(四、条件)
查看>>
阿里云MWC 2019发布7款重磅产品,助力全球企业迈向智能化
查看>>
使用Logtail采集Kubernetes上挂载的NAS日志
查看>>
电脑录音软件哪个好,怎么用电脑录音
查看>>
《前端十年-我将一切告诉你》人物关系图
查看>>
angular js中的依赖注入是什么?
查看>>
聊聊 Array 中的坑
查看>>
修改golang源代码获取goroutine id实现ThreadLocal
查看>>
Flutter尝鲜2——动画处理<基础>
查看>>
【Redis源码分析】Redis的压缩列表ZipList
查看>>
【学习笔记】CSS深入理解之line-height
查看>>
41. 缺失的第一个正数
查看>>
【C++】 47_父子间的冲突
查看>>
[LeetCode] 694. Number of Distinct Islands
查看>>
文章收藏夹
查看>>
PHP设计模式(五)建造者模式(Builder)
查看>>
关于如何在Python中使用静态、类或抽象方法的权威指南
查看>>