-1

I've been learning about classes in JavaScript, the use of prototype and finally how to inherit.

From my understanding the below should:

  1. Alert "John" due to myInstance.getIt(); being called
  2. Alert "Jack" due to myInheritedInstance.getIt(); being called
  3. myInheritedInstance.getParent(); has been assigned to .getIt() in MyClass
  4. This should then alert "John" when myInheritedInstance.getParent(); is called.

Instead what actually happens is:

  1. Alert "John"
  2. Alert Blank
  3. Alert "Jack"

I have a feeling I've done something stupid or have misunderstood a fundamental concept here, so any help would be appreciated.

var MyClass = function() { };
MyClass.prototype.constructor = MyClass;
MyClass.prototype.name = "John";
MyClass.prototype.getIt = function () { alert(this.name); };
var myInstance = new MyClass();
myInstance.getIt();
//Now inheritance
var MyInheritedClass = function () { };
MyInheritedClass.prototype = new MyClass;
MyInheritedClass.prototype.constructor = MyInheritedClass;
MyInheritedClass.prototype.name = "Jack";
MyInheritedClass.prototype.getIt = function () { alert(this.name); };
MyInheritedClass.prototype.getItParent = MyClass.prototype.getIt.call(this);
var myInheritedInstance = new MyInheritedClass();
myInheritedInstance.getIt();
myInheritedInstance.getItParent();
asked May 23, 2012 at 17:12

1 Answer 1

3

The culprit is:

MyInheritedClass.prototype.getItParent = MyClass.prototype.getIt.call(this);

.call will call a function, not return one. So it will cause two issues: it calls it beforehand, and returns something that isn't callable (you get an error in the console). You'd have to do:

MyInheritedClass.prototype.getItParent = function() {
 alert(Object.getPrototypeOf(Object.getPrototypeOf(this)).name);
};

The problem is that name is not accessible through this anymore as it has been shadowed over by the inherited class. To get name of the original class, you have to walk up the prototype chain two times: inherited instance -> inherited prototype -> original prototype.

The line

MyClass.prototype.constructor = MyClass;

isn't necessary here, by the way. Restoring constructor is necessary in case you overwrite prototype, because constructor gets lost in that case. So in your case, it is necessary only for the inherited class.

Also, the line

MyInheritedClass.prototype.getIt = function () { alert(this.name); };

is superfluous, it's just the same as MyClass.prototype.getIt - which you inherited.

Note that JavaScript does not have real "classes", although their behaviour can be accomplished like this.

answered May 23, 2012 at 17:18
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the clarification. I think the issue is that I've been working from a not-so-correct tutorial. So there is no way to get at the base classes overridden methods and values via call? Is the example you show the only way to "get at" them?
And yes, I think that the addition of getIt within MyInheritedClass was my own little "faux pas" that I've brought into the situation!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.