oop - How to call parent class' method from a subclass in JavaScript so that parent's local variables would be accessible? -


i'm using 1 of approaches class inheritance in javascript (as used in code i'm modifying), not understand how attach additional functionality method in subclass functionality respective parent class method has; in other words, want override parent's method in child class method besides own sub-class-specific stuff same parent's method doing. so, i'm trying call parent's method child's method, possible?

the code here: http://jsfiddle.net/7zmnw/. please, open development console see output.

code here:

function makeassubclass (parent, child) {     child.prototype = new parent;   // no constructor arguments possible @ point.     child.prototype.baseclass = parent.prototype.constructor;     child.prototype.constructor = child;      child.prototype.parent = child.prototype; // 2nd way of calling methodb. }  function parent (invar) {     var parentvar = invar;      this.methoda = function () {console.log("parent's methoda sees parent's local variable:", parentvar);};     this.methodb = function () {console.log("parent's methodb doesn't see parent's local variable:", parentvar);}; }  function child (invar) {     child.prototype.baseclass.apply(this, arguments);      this.methodb = function ()     {         console.log("child's method start");         child.prototype.methodb.apply(this, arguments); // 1st way         this.parent.methodb.apply(this, arguments); // 2 2nd way         console.log("child's method end");     }; }  makeassubclass(parent, child);  var child = new child(7); child.methoda(); child.methodb(); 

no can't see parents local variables. inherit parents prototype chain, not local state. in case you're applying parent function onto child object not hold state.

apply(this,...) 

means you're binding function current value of this. when call method b child object, bound child, , therefore not operating within closure contains parents value.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -