Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Created January 4, 2012 07:16
Show Gist options
  • Save jarek-foksa/1558929 to your computer and use it in GitHub Desktop.
Save jarek-foksa/1558929 to your computer and use it in GitHub Desktop.
//
// "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