Created
January 4, 2012 07:16
-
-
Save jarek-foksa/1558929 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// "Framework" | |
// | |
window.object = Object.prototype; | |
Object.define = function(arg) { | |
var prototype = arg.__prototype__ || Object.prototype; | |
var object = Object.create(prototype); | |
var property; | |
for (property in arg) { | |
if (arg.hasOwnProperty(property)) { | |
object[property] = arg[property]; | |
} | |
} | |
delete object.__prototype__; | |
return object; | |
} | |
// | |
// Example | |
// | |
var animal = Object.define({ | |
__prototype__: object, | |
init: function(name) { | |
this.name = name; | |
return this; | |
}, | |
move: function(meters) { | |
console.log(this.name + " moved " + meters + "m."); | |
}, | |
}); | |
var snake = Object.define({ | |
__prototype__: animal, | |
move: function() { | |
console.log("Slithering..."); | |
animal.move.apply(this, arguments); | |
}, | |
}); | |
var horse = Object.define({ | |
__prototype__: animal, | |
move: function() { | |
console.log("Galloping..."); | |
animal.move.apply(this, arguments); | |
}, | |
}); | |
var sam = Object.create(snake).init("Sammy the Python"); | |
var tom = Object.create(horse).init("Tommy the Palomino"); | |
sam.move(10); | |
tom.move(120); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment