Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.