Created
February 27, 2013 07:21
object creation problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = Object.create(null); | |
// Here we are reusing the previous gettersetter functions | |
Object.defineProperty(person, 'name', { get: get_full_name , set: set_full_name , configurable: true, enumerable: true }); | |
// And adding the `greet' function | |
person.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.create(person); | |
mikhail.first_name = 'Mikhail'; | |
mikhail.last_name = 'Weiß'; | |
mikhail.age = 19; | |
mikhail.gender = 'Male'; | |
// And we can test whether things are actually working. | |
// First, `name' should be looked on `person' | |
alert(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'var person = Object.create(null); | |
// Here we are reusing the previous gettersetter functions | |
Object.defineProperty(person, 'name', { get: get_full_name , set: set_full_name , configurable: true, enumerable: true }); | |
// And adding the `greet' function | |
person.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.create(person); | |
mikhail.first_name = 'Mikhail'; | |
mikhail.last_name = 'Weiß'; | |
mikhail.age = 19; | |
mikhail.gender = 'Male'; | |
// And we can test whether things are actually working. | |
// First, `name' should be looked on `person' | |
alert(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' ] | |
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' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment