Subj : Re: accessing methods of base class To : netscape.public.mozilla.jseng From : Brendan Eich Date : Sun Jul 20 2003 12:45 pm mike caines wrote: > Referring to example: > > function One() { > this.toString = function() {return 'One';}; > } It's better to share methods via the prototype, so: function One() {} One.prototype.toString = function () {return 'One';}; > > function Two() { > One.apply(this); > this.toString = function() {return + '\n' + > 'Two';} > } > Two.prototype = new One(); Likewise: function Two() { /* you could call One.apply(this); here */ } Two.prototype = new One; Two.prototype.toString = function () { return One.prototype.toString() + '\nTwo'; } > Is it somehow possible to access the original toString method of the base > class once it has been overridden? So that the overriding version can > 'append' to the original? (see example). There are several ways; I've shown one above. You could also give the base toString method a name in the global object, by declaring it as a top-level function (instead of writing a function expression). Or in SpiderMonkey and Rhino, you could say this.__proto__.toString() in Two's toString body. > Also, doesn't the Two.prototype = new One(); statement result in the One > constructor being executed twice? No. Why would 'new One()' call the One constructor twice? /be .