2

Could you explain me why necessary (or recommended) to use “__proto__” and “prototype” in JavaScript inheritance? Here are two code examples and it seems that their result is exactly the same with and without using of prototype. The result is the following in both cases:

"elephant is walking to melbourne"

"sparrow is walking to sydney"

"sparrow is flying to melbourne"

Example one:

function Animal(name) {
    this.name = name;
}

Animal.prototype.walk = function (destination) {
    console.log(this.name, 'is walking to', destination);
};

var animal = new Animal('elephant');
animal.walk('melbourne');

function Bird(name) {
    Animal.call(this, name);
}

Bird.prototype.__proto__ = Animal.prototype;

Bird.prototype.fly = function (destination) {
    console.log(this.name, 'is flying to', destination);
}

var bird = new Bird('sparrow');
bird.walk('sydney');
bird.fly('melbourne');

Example two:

function Animal(name) {
    this.name = name;
	
	this.walk = function (destination) {
		console.log(this.name, 'is walking to', destination);
	};
}

var animal = new Animal('elephant');
animal.walk('melbourne');

function Bird(name) {
    Animal.call(this, name);
	
	this.fly = function (destination) {
		console.log(this.name, 'is flying to', destination);
	}
}

var bird = new Bird('sparrow');
bird.walk('sydney');
bird.fly('melbourne');

For example why "Bird.prototype.fly = function..." is better than simple "this.fly = function..." in Bird function?

6

1 Answer 1

1

I think this should make it plenty clear. I've taken the animal out of the script (er... literally).

function Bird(name) {
    this.name = name;
    this.fly = function (destination) {
        console.log(this.name, 'is flying to', destination);
    }
}

var bird = new Bird('sparrow');
bird.fly('Melbourne');
bird.fly = function (destination) {
    console.log(this.name, 'is taking the plane to', destination);
}
bird.fly('Melbourne');

var bird2 = new Bird('eagle');
bird2.fly('Melbourne');

which gives

sparrow is flying to Melbourne

sparrow is taking the plane to Melbourne

eagle is flying to Melbourne

vs.

function Bird(name) {
    this.name = name;
}
Bird.prototype.fly = function (destination) {
    console.log(this.name, 'is flying to', destination);
}

var bird = new Bird('sparrow');
bird.fly('Melbourne');
Bird.prototype.fly = function (destination) {
    console.log(this.name, 'is taking the plane to', destination);
}
bird.fly('Melbourne');

var bird2 = new Bird('eagle');
bird2.fly('Melbourne');

which gives

sparrow is flying to Melbourne

sparrow is taking the plane to Melbourne

eagle is taking the plane to Melbourne

In the first case you are modifying that object's fly function. In the 2nd case, you are modifying a common shared function (from the prototype)

Since you mostly want the function to be common (and the data separate), you usually use Bird.prototype....

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

2 Comments

It's clear although it isn't answer to my question.One of my question is at the end of my post. Why "Bird.prototype.fly = function..." is better than simple "this.fly = function..." in Bird function?
Because in most cases you want a "common" function for the Bird class instead of duplicate functions for the bird class. Much like if you were having a base class and 2 derived classes, you'd put a common function in the base class instead of duplicating it in the 2 inherited classes. In javascript your prototype would be your base (class) and your 2 Bird instances would be your inherited (instances)

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.