Subclassing and the prototype chain
Date : 2008 03 18 Category : Tech & DevelopmentAre you sure you should be subclassing that? is the question that Neil Roberts asks.
He goes on to solve a problem: you just want to change ONE property in this class, but you can’t change it on the actual class because that value would now be used across all instances of that class. So in order to make it happen, you go through all the messy, painful steps described above.
But wouldn’t it be nice if we could just do this:
PLAIN TEXT JAVASCRIPT:var Child = sp.clone(Parent, { foo: "new value" });
He walks through the implementation, teaching you more and more about prototype based inheritance as you go. You have to go through the steps to really understand how he ends up with this:
PLAIN TEXT JAVASCRIPT:sp.clone = function(superclass, mixins){ var original = superclass.__original__ || superclass; var f = function(){ original.apply(this, arguments); } f.__original__ = superclass; var unchain = arguments[arguments.length-1] === true; if(unchain){ f.prototype = dojo.mixin({}, superclass.prototype); }else{ dojo.delegate(superclass.prototype); } for(var i=1, arg, l=arguments.length-unchain, args=[f.prototype]; i<l ;> arg = arguments[i]; args[i] = (typeof arg == "function") ? arg.prototype : arg; } dojo.mixin.apply(null, args); f.prototype.constructor = f; return f; }