I am trying to learn JavaScript object inheritance. I am referring to JavaScript Cookbook by Shelley Powers.
In the subclass you need to call superclass.apply(this,arguments) to use its properties. And according to the book I also need to write something like subclass.prototype = new superclass();
However, I noted that things work without using the subclass.prototype = new superclass(); statement. Below is my code. What is the purpose of subclass.prototype = new superclass();
var Book = function (newTitle, newAuthor) {
var title;
var author;
title = newTitle;
author = newAuthor;
this.getTitle = function () {
return title;
}
this.getAuthor = function () {
return author;
}
};
var TechBook = function (newTitle, newAuthor, newPublisher) {
var publisher = newPublisher;
this.getPublisher = function () {
return publisher;
}
Book.apply(this, arguments);
this.getAllProperties = function () {
return this.getTitle() + ", " + this.getAuthor() + ", " + this.getPublisher();
}
}
//TechBook.prototype = new Book(); // Even when commented,
var b1 = new TechBook("C Pro", "Alice", "ABC Publishing");
var b2 = new TechBook("D Pro", "Bob", "DEF Publishing");
alert(b1.getAllProperties());
alert(b2.getAllProperties());
alert(b1.getTitle());
alert(b2.getTitle());