4

I was reading Introduction to Object-Oriented JavaScript from Mozilla Developer Network, time to learn so serious Javascript before start using node.js.

Anyway, inheritance thing seems so obscure to me. Copy and paste from the documentation:

// define the Person Class
function Person() {}

Person.prototype.walk = function(){
  alert ('I am walking!');
};
Person.prototype.sayHello = function(){
  alert ('hello');
};

This is easy, but things get complicated with Student inheritance. Does anyone else think that the following three statements do essentially the same thing?

// define the Student class
function Student() {
  // Call the parent constructor
  Person.call(this);
}

// inherit Person
Student.prototype = new Person();

// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;

I understand the first one (calling the parent constructor) because is so similar to Java, PHP and so on. But then problems begin.

Why there is the need of calling Student.prototype and Student.prototype.constructor?

A clear explanation is needed. Why this code:

// define the Student class
function Student() {
  // Call the parent constructor
  Person.call(this);
}

var student1 = new Student();

is not enough for inheritance to work?

EDIT: regarding the constructor thing, it has been already answered here.

Community
  • 1
  • 1
gremo
  • 47,186
  • 75
  • 257
  • 421
  • 3
    possible duplicate of [Why is it necessary to set the prototype constructor?](http://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor) – pimvdb Aug 31 '12 at 14:43
  • "Object-Oriented JavaScript" doesn't exists, Javascript is Prototype Oriented and you must adapt your Object-Oriented approaches to Prototypes. – Chiguireitor Aug 31 '12 at 14:47
  • 1
    @Chiguireitor i didn't write the documentation title, you know... – gremo Aug 31 '12 at 14:48
  • @pimvdb you're partially right. My question overlaps that posted by you for the constructor thing. But it's not exactly the same: i'd like to know why the second statement is needed. – gremo Aug 31 '12 at 14:51

2 Answers2

2

This doesn't work:

function Student() {
  Person.call(this);
}

var student1 = new Student();

because Person's prototype properties are not available on Student instances. Person.call(this) merely calls Person with a specific this value (namely the Student instance). But the Person function is completely empty - so here it doesn't do anything. There is no relation between Student and Person other than the useless Person call.

To get Person's functions the .prototype assignment is necessary.

Before:

<a Student instance>
  its prototype: Student.prototype, with all student functions
    its prototype: Object.prototype

After:

<a Student instance>
  its prototype: <a Person instance>, with all student functions if you add them
    its prototype: Person.prototype, with all person functions
      its prototype: Object.prototype
pimvdb
  • 151,816
  • 78
  • 307
  • 352
1

If you're using Node.js, I would suggest looking into ECMAScript 5 methods for object creation. MDN has a page here which might be a good guide. See also John Resig's overview of the new method. Prototypal inheritance is hard to get your head around when coming from a classical OO language like Java. Good luck!

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122