3

I'm somewhat new to JavaScript. I know that you should use prototype to implement inheritance between objects, but I tried the following and it worked perfectly (using Visual Studio 2012). What am I doing wrong?

function Person(firstname, lastname, age, eyecolor) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    this.eyecolor = eyecolor;

    this.name = function() {
        return this.firstname + " " + this.lastname;
    }
}

function Student(firstname, lastname, age, eyecolor, level) {
    this.level = level;
    Person.call(this, firstname, lastname, age, eyecolor);
}

var per = new Person("Abe", "Lincoln", 45, "green");

var obj = new Student("Abe", "Lincoln", 45, "green", "senior");

When I examine obj, it has properties for Person and for Student, and I can call obj.name() to get "Abe Lincoln". Even in the Visual Studio immediate window, I can see all of the properties as siblings of one another, as I would expect. But I am not using prototype, so obviously this is not right.

Set me straight please :)

Thanks in advance!

1
  • 1
    You're doing something more like "mixing" two classes. (Really, the word "class" itself is somewhat misleading when talking about JavaScript.) Commented Feb 16, 2013 at 21:44

1 Answer 1

1

To use prototypal inheritance, you'd put the name method on Person.prototype:

function Person(firstname, lastname, age, eyecolor) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    this.eyecolor = eyecolor;
}
Person.prototype.name = function() {
    return this.firstname + " " + this.lastname;
}

Then make the .prototype object of Student an instance of Person:

function Student(firstname, lastname, age, eyecolor, level) {
    this.level = level;
    Person.call(this, firstname, lastname, age, eyecolor);
}

// Make the prototype object of the Student constructor inherit from the
//    prototype object of the Person constructor.
Student.prototype = Object.create(Person.prototype)

And so now your name method is shared among all instances created instead of being remade for each instance.

var per = new Person("Abe", "Lincoln", 45, "green");

var obj = new Student("Abe", "Lincoln", 45, "green", "senior");
Sign up to request clarification or add additional context in comments.

7 Comments

this is the first example of prototypical inheritance I've seen that actually works the way I'd expect it to. I had thought I had read that it wasn't possible to overwrite the .prototype like that, though.
@Alnitak: Nope, the prototype object can be changed at any time. Any previous objects created using the constructor will still reference the old prototype object, but any objects created after the change will use the new one. One thing you lose is the .constructor property on the original prototype object that points back to the constructor function, but that can be replaced if desired.
Thank you. So, I use prototype for the method(s), but not for the properties, unless I want one of the properties to be static across all instances, right? Would I also remove the "Person.call(this,...) invocation inside the Student? Or does that happen via the last line of your code example?
@user2079283: Right. You can use it for other properties if the values are primitives. When you go to set the value on the actual object, the property will shadow the prototyped one. You don't want to put Objects and Arrays on the .prototype, because mutations will be visible by all the objects created from the constructor.
...no, don't remove the Person.call.... That calls the Person constructor on each Student object. The one I added just creates an empty object that is used for inheritance.
|

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.