0

Being from the classical Inheritance Background(C#,Java etc.) , I am struggling with the Prototypal Way of doing it. I am not understanding the basics also . Please explain and correct me on the following code block.

    var util = require ('util'); 

function Student(choiceOfStream) { 
    this.choiceOfStream = choiceOfStream;
}

Student.prototype.showDetails = function() {
    console.log("A student of "+this.choiceOfStream+" has a major in "+this.MajorSubject);
}

function ScienceStudent() {
    Student.call(this,"Science");
}

function ArtsStudent() {
    Student.call(this,"Arts");
}

util.inherits(ScienceStudent,Student);
util.inherits(ArtsStudent,Student);

var ArtsStudent = new ArtsStudent(); 
var ScienceStudent = new ScienceStudent(); 

ScienceStudent.prototype.MajorSubject = "Math";
ArtsStudent.prototype.MajorSubject = "Literature";

console.log(ArtsStudent.showDetails());
console.log(ScienceStudent.showDetails());

But the error I am getting is enter image description here

What was I missing ?

0

1 Answer 1

1

There is no standard this.super_ property so I'm not sure where you got that from. If you're using util.inherits(), you can see a nice simple example of how to use it in the nodejs doc for util.inherits().

And, here's how your code could work:

var util = require ('util');

function Student(choiceOfStream) { 
    this.choiceOfStream = choiceOfStream;
}

Student.prototype.showDetails = function() {
    console.log("A student of "+this.choiceOfStream+" has a major in "+this.MajorSubject);
}

function ScienceStudent() {
    Student.call(this, "Science");
    this.majorSubject = "Math";
}

function ArtsStudent() {
    Student(this,"Arts");
    this.majorSubject = "Literature";
}

util.inherits(ScienceStudent,Student);
util.inherits(ArtsStudent,Student);

FYI, in ES6 syntax, there is a super keyword which is part of a new way of declaring Javascript inheritance (still prototypal) you can read about here.

Sign up to request clarification or add additional context in comments.

6 Comments

But still gives me an error ... saying cannot set property "MajorSubject" for undefined . Please see the edited question.
@StrugglingCoder - Because you are still apparently trying to use the .super_ property which does not exist. See the initializer for .majorSubject I added to each constructor. In general, function properties (e.g. methods) should be on the prototype and data properties should be on the object itself initialized in the constructor. This is because things on the prototype are shared between ALL instances of that object type which is perfect for methods, but is usually not what you want for data.
Why can't I just add it to the respective prototypes ? Got it . Thanks
@StrugglingCoder - See what I had already added to my previous comment. You can put data properties on the prototype, but it is not the usual practice because it does not usually behave the way people want data properties to behave.
@StrugglingCoder - your next problem is that when you do this: var ArtsStudent = new ArtsStudent(); , you are reassigning the constructor function. Use a different name for your instance variable. A good convention to follow is that Constructor functions start with uppercase and all other properties or variables start with lowercase.
|

Your Answer

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.