Object.create is a great addition to JavaScript, because it adheres more to the prototypical nature of JS. However, I can’t help but find the syntax of the 2nd parameter to the function to be too verbose, and a step back.
For example, if I want to create an object, and specify a new property in the derived object, I need to include that property value within a property object, regardless if I’m interested in the extra features or not.
So, something as simple as this:
o = Object.create({}, { p: 42 })
Now becomes:
o = Object.create({}, { p: { value: 42 } })
Obviously this is a simple example, but to me the verbosity is unnecessary, and should be optional.
Does anyone understand the decision to require a properties object? What is your opinion of the requirement of the new syntax?
Note: I understand there are easy solutions to overcome this requirement.
The syntax is done this way so that you can add parameters that control each property:
So, when you do this:
you are saying that you want a property named
pwith a value of42. The key here is that there are other parameters you can set for each property and if there wasn’t this extra level of object hierarchy, you wouldn’t have any way to pass those extra parameters.So, for example, you could also do this:
Here’s you are not just specifying the value of
42, but also some options for that property. If there wasn’t the extra level of object hierarchy here, then you wouldn’t have a place to put those extra options.Yes, it does seem inconvenient when you only want the simple case. But, you could easily write yourself a helper function that made the simpler syntax work:
Demo here: http://jsfiddle.net/jfriend00/vVjRA/