faqts : Computers : Programming : Languages : JavaScript : Language Core : prototypes/inheritance

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

Is it possible to call a method in a super class?
Another way to have (call) a super-mathod

Nov 8th, 2000 09:17
alex dent, other faqts articles


this is actuallay a merged version of two other articles here.

you can get better access to the original methods of the superclass if 
you copy all elements of the superclass with a new name, i added 
'super_'. this function also calls the constructor of the superclass with 
the supplied arguments, and is availiable in any new object.

Object.prototype.saveSuper = function () {
	// netscape 4 and 6 only 
    // this.constructor.apply(this, arguments);
	
    // ie 4 and 5 and netscape 4 and 6
    var c = 'this.constructor(';
	for(i = 0; i < arguments.length; i++) c += 'arguments[' + i + '],';
	eval(c.substring(0, c.length - 1) + ');');
	
    for(p in this) {
		if(p.indexOf('super_') == -1) this['super_' + p] = this[p];
	}
}

function n2(x) {
    this.n1 = function () {
        alert(this.x * 2);
    }
    this.x = x;
}
function n0(x)
{ 
    this.saveSuper(x);
    this.n1 = function () {
        this.super_n1();
        alert(this.x);
    }
}
n0.prototype = new n2;
x = new n0(99);

x.n1(); // alerts 198 and 99