3

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());
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Cracker
  • 1,780
  • 4
  • 24
  • 34
  • possible duplicate of [How does JavaScript .prototype work?](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – hvgotcodes May 25 '11 at 14:26
  • @hvgotcodes: Not a duplicate. I know the concept of prototype but my code is working without it. I want to know how that is possible. – Cracker May 25 '11 at 14:31

2 Answers2

4

It works because you are adding the functions in the Book constructor to this. So whatever object you pass to Book via apply, the methods are set on this object.

But TechBook does not have the methods Book might have through its prototype.

E.g. if you had something like:

Book.prototype.getTitle = function()...

then TechBook would not have this function unless you set TechBook's prototype correctly.


By setting the functions in the constructor, you create new instances of each method for every instance you create. A prototype is used to avoid that, so that several instances can share code.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Ok great! So now I am planning NOT to use prototype for my subclasses so I can inherit them without using the prototype statement. Any disadvantages of this approach? – Cracker May 25 '11 at 14:43
  • I don't like using subclass.prototype = new superclass() as it does not look analogous to OOP languages like Java and C++ – Cracker May 25 '11 at 14:45
  • 2
    @Cracker: The one I already mentioned: It will use more memory, depending on how many instances you create. There is a difference between whether a function is only defined once or a hundred times. You have to look at the `subclass.prototype = new superclass()` as `ClassA extends ClassB`... it's just how it works in JavaScript. – Felix Kling May 25 '11 at 14:47
1

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();

I don't have that book. Javascript doesn't have classes, best to forget about them for the time being and just concentrate on how prototype inheritance works. You can emulate class-based inheritance, but it is usually not necessary or even desirable.

Your code doesn't use prototype inheritance, it creates properties on each instance created by the constructors.

When you call Book from within TechBook, it isn't called as a constructor, it's called as a function and passed the this object from TechBook, so properties are added directly to that object.

The object returned by new TechBook inherits from TechBook.prototype, but you haven't set any properties on it.

The code pattern seems to be using constructors to emulate private and privileged members using closures, the module pattern is more commonly used for that.

Lastly, Book and TechBook would be more appropriately be written as function declarations rather than expressions.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Had a look at the module pattern. But looks like it is suitable when you want to create a single object. I want something like classes of which I can create many instances. – Cracker May 25 '11 at 15:14