Created
August 14, 2011 03:46
-
-
Save zaach/1144539 to your computer and use it in GitHub Desktop.
An example from http://yehudakatz.com/2011/08/ adapted to use the <| operator
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 fromPrototype = function(prototype, object) { | |
var newObject = Object.create(prototype); | |
for (var prop in object) { | |
if (object.hasOwnProperty(prop)) { | |
newObject[prop] = object[prop]; | |
} | |
} | |
}; | |
var person = { | |
toString: function() { | |
return this.firstName + ' ' + this.lastName; | |
} | |
}; | |
var man = fromPrototype(person, { | |
sex: "male" | |
}); | |
var jeremy = fromPrototype(man, { | |
firstName: "Jeremy", | |
lastName: "Ashkenas" | |
}); | |
jeremy.sex // "male" | |
jeremy.toString() // "Jeremy Ashkenas" |
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 = { | |
toString: function() { | |
return this.firstName + ' ' + this.lastName; | |
} | |
}; | |
var man = person <| { | |
sex: "male" | |
}; | |
var jeremy = man <| { | |
firstName: "Jeremy", | |
lastName: "Ashkenas" | |
}; | |
jeremy.sex // "male" | |
jeremy.toString() // "Jeremy Ashkenas" |
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
// This version demonstrates a reference to super | |
var person = { | |
toString: function() { | |
return this.firstName + ' ' + this.lastName; | |
} | |
}; | |
var man = person <| { | |
sex: "male", | |
toString: function() { | |
return "Mr. " + super.toString(); | |
} | |
}; | |
var jeremy = man <| { | |
firstName: "Jeremy", | |
lastName: "Ashkenas" | |
}; | |
jeremy.sex // "male" | |
jeremy.toString() // "Mr. Jeremy Ashkenas" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment