7

In what usages of Object.create do you want to set enumerable to true?

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 Answers1

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