In what usages of Object.create do you want to set enumerable
to true
?
Asked
Active
Viewed 2,348 times
7

Mike Samuel
- 118,113
- 30
- 216
- 245

ryanve
- 50,076
- 30
- 102
- 137
-
1[Emulate non-enumerable properties](http://stackoverflow.com/questions/8918030/is-it-possible-to-emulate-non-enumerable-properties) – Raynos Jan 27 '12 at 21:18
1 Answers
13
A property of an object should be enumerable if you want to be able to have access to it when you iterate through all the objects properties. Example:
var obj = {prop1: 'val1', prop2:'val2'};
for (var prop in obj){
console.log(prop, obj[prop]);
}
In this type of instantiation, enumerable is always true, this will give you an output of:
prop1 val1
prop2 val2
If you would have used Object.create() like so:
obj = Object.create({}, { prop1: { value: 'val1', enumerable: true}, prop2: { value: 'val2', enumerable: false} });
your for loop would only access the prop1, not the prop2. Using Object.create() the properties are set with enumerable = false by default.

André Alçada Padez
- 10,987
- 24
- 67
- 120
-
-
thanks, i was referring to the other instantiation. i forgot to mention that. I will edit my answer – André Alçada Padez Jan 27 '12 at 21:25