I don’t know the key to success, but the key to failure is trying to please everybody.

Menu

ECMA5 features and why they might make sense

Today, I asked on LinkedIn “Under which circumstances I should use { enumerable : false } ? Just to prevent the appearance of the property in .keys()?”.

The answer: “A good example of something that shouldn’t be enumerable is the Array length property. It’s a property that shouldn’t be iterated over or treated as part of the collection. So, say you have a custom object that can be iterated through, but you also want to attach some metadata such as, for example, an Animals collection that has properties like .numberOfMammals, and .numberOfReptiles. You could still use Object.keys to iterate through all the animals, but you can also have properties that aren’t considered part of the collection.”

Consider these examples. The first one is implemented as a Map class and it’s kind of “static”. The second one inherits from Array prototype and adds the add method and both properties numberOfMammals and numberOfReptiles as well. Consider that both properties aren’t enumberable.

Actually the advantages of the second implementation are:

we can use the existing collection class: Array

people don’t need to know something about the proprietary implementation (like this.arr
in the first example)

we are enabled to hide the implementation details by using enumerable=false

Note. You might have recognized that we implemented a new method add just to insert our data instead of the inherited push method. I would like to get rid of it… Is there a “beforePush” event so that I could set the properties numberOfMammals and numberOfReptiles properly?