Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript class extension : calling base class-function from child-class function : seem to be calling static method
    text
    copied!<p>This is complicated to explain but I've been on this for days now. I'm "extending" one Javascript "class" to another but calling a base-class method from a child-class method does not work properly. It seems as if the base-class-method "acts" as a static method : properties that are defined in the childclass somehow have their default values when calling a base-class-method from a-child-class method. </p> <p>To keeps things simple I didn't include my elaborate code but created a simple example of what I'm doing:</p> <pre><code>function extend(Child, Parent){ var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.base = Parent.prototype; } function BaseClass(){ } BaseClass.prototype.randomNumber = 0; BaseClass.prototype.functionA = function(){ this.randomNumber = 200; this.functionB(); } BaseClass.prototype.functionB = function(){ alert(this.randomNumber); console.info(this); } function ChildClass(){ this.functionB = function(){ ChildClass.base.functionB(); }; } extend(ChildClass, BaseClass); var childInstance = new ChildClass(); childInstance.functionA(); </code></pre> <p>I'm using firebug to log to the console. It output's "Baseclass" and this.randomNumber is somehow 0 while it is set to 200 in functionA.</p> <p>the extend-method I'm using is simular to the one from Yahoo.lang.extend which I also tried with the same result.</p> <p>I can't figure out if I'm missing something or this is just one of the drawbacks from "extending" Javascript and I'm actually calling a "static" method here. Help would be much appreciated here. Hope it's clear what I'm trying to do here because I find it complicated to put it in words. Thanks a lot in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload