Advertisement
Advertisement
| 02.25.2008 at 09:04AM PST, ID: 23190796 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: |
function extendClass(subClass, superClass){
function F(){};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superClass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
// insert call to superClass constructor
var constructorStr = subClass.prototype.constructor + ""; // convert to string
var splitAt = constructorStr.indexOf('{') + 1;
var start = constructorStr.substring(0, splitAt);
var end = constructorStr.substring(splitAt, constructorStr.length);
var newMethod = "arguments.callee.superClass.constructor.call(this);";
var newConstructor = start + newMethod + end;
subClass.prototype.constructor = eval(newConstructor);
}
// ------------- BEGIN BaseObject ------------- //
function BaseObject() {
}
BaseObject.prototype.method = function() {
alert("Alert from BaseObject.method");
}
// ------------- END BaseObject ------------- //
// ------------- BEGIN InheritsBaseObject ------------- //
function InheritsBaseObject() {
}
extendClass(InheritsBaseObject, BaseObject);
InheritsBaseObject.prototype.method = function() {
alert('I overrode BaseObject.method.');
}
// ------------- END InheritsBaseObject ------------- //
var x = new InheritsBaseObject();
x.method();
|