When creating JavaScript objects with properties, why is this code necessary?

javascript, object, properties, prototype

Solution

Because the parameter is a properties object. You're not just defining fields, you're defining properties which is a bit of a different animal. For example you could specify the 'age' isn't writable. Refer to this documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

Problem

I am reading about JavaScript prototypes here. Under the Object.create header, some code is written out to illustrate creating objects with prototypes and certain properties: ``` var person = { kind: 'person' } // creates a new object which prototype is person var zack = Object.create(person); console.log(zack.kind); // => ‘person’ ``` I then encountered this: ``` var zack = Object.create(person, {age: {value: 13} }); console.log(zack.age); // => ‘13’ ``` Instead of passing `{age: {value: 13} }`, I passed `{age: 13}` because it seemed simpler. Unfortunately, a `TypeError` was thrown. In order to create properties of this object like this, why do we have to pass `{age: {value: 13} }` rather than just `{age: 13}`?

Original source