Created
October 30, 2011 13:49
-
-
Save Raynos/1325915 to your computer and use it in GitHub Desktop.
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
// 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"; | |
} |
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 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" |
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 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