4

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}?

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.