
Untitled
By: a guest on
Jun 2nd, 2012 | syntax:
None | size: 1.10 KB | hits: 10 | expires: Never
var person = Object.make(null, {
get name() {
return this.first_name + " " + this.last_name;
},
set name() {
...
},
greet: function (person) {
return this.name + ': Why, hello there, ' + person + '.'
}
})
// Then we can share those behaviours with Mikhail
// By creating a new object that has it's [[Prototype]] property
// pointing to `person'.
var mikhail = Object.make(person, {
first_name: 'Mikhail',
last_name: 'Weiß',
age: 19,
gender: 'Male'
})
// And we can test whether things are actually working.
// First, `name' should be looked on `person'
mikhail.name
// => 'Mikhail Weiß'
// Setting `name' should trigger the setter
mikhail.name = 'Michael White'
// Such that `first_name' and `last_name' now reflect the
// previously name setting.
mikhail.first_name
// => 'Michael'
mikhail.last_name
// => 'White'
// `greet' is also inherited from `person'.
mikhail.greet('you')
// => 'Michael White: Why, hello there, you.'
// And just to be sure, we can check which properties actually
// belong to `mikhail'
Object.keys(mikhail)
// => [ 'first_name', 'last_name', 'age', 'gender' ]