Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created October 30, 2011 13:49
Show Gist options
  • Save Raynos/1325915 to your computer and use it in GitHub Desktop.
Save Raynos/1325915 to your computer and use it in GitHub Desktop.
// But I don't want a direct reference to the Parent class in my code!!
var super = function (obj) {
return Object.getPrototypeOf(obj);
}
var Child = Object.create(Parent);
Child.hierachy = function () {
return super(this).hierachy.call(this) + " < C";
}
var Parent = {
hierachy: function () {
return "P";
},
constructor: function (thing) {
this.thing = thing;
}
}
var Child = Object.make(Parent, {
hierachy: function () {
return Parent.hierachy.call(this) + " < C";
},
constructor: function (thing, other) {
Parent.constructor.call(this, thing);
this.other = other;
}
});
var GrandChild = Object.make(Child, {
hieracy: function () {
return Child.hierachy.call(this) + " < GC";
},
constructor: function (thing, other) {
other = other * 2;
Child.constructor.call(this, thing, other);
}
});
var gc = Object.create(GrandChild);
gc.constructor("thing", 42);
// or var gc = new (GrandChild.constructor)("thing", 42);
alert(gc.hierarchy()); // Alerts "P < C < GC"
var Parent = {
hierachy: function () {
return "P";
}
}
// Yes I cheated, I used [pd.make](https://github.com/Raynos/pd#pd.make) as sugar here. Alternatively
/*
var Child = Object.create(Parent);
Child.hierachy = function () {
return Parent.hierachy.call(this0 + " < C";
}
*/
var Child = Object.make(Parent, {
hierachy: function () {
return Parent.hierachy.call(this) + " < C";
}
});
var GrandChild = Object.make(Child, {
hieracy: function () {
return Child.hierachy.call(this) + " < GC";
}
});
var gc = Object.create(GrandChild);
alert(gc.hierarchy()); // Alerts "P < C < GC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment