Prototype chaining in JavaScript
For your first point:
What this guy is trying to say, is that the methods for both child and parent will change if you modify the prototype after you created instances.
For example:
function inherit(C, P) {
C.prototype = P.prototype;
}
function Parent(){}
function Child(){}
inherit(Child, Parent);
Parent.prototype.say = function () {
return 20;
};
var parent = new Parent();
var child = new Child();
// will alert 20, while the method was set on the parent.
alert( child.say() );
The same thing happens when you change the child's constructor (which is shared with the parent).
// same thing happens here,
Child.prototype.speak = function() {
return 40;
};
// will alert 40, while the method was set on the child
alert( parent.speak() );
And about your second point:
function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
}
The new inheriting function will actually separate the constructor of the parent from the child, because it's not pointing to the same object anymore, but now it's pointing to a prototype of a newly created function that has nothing to do with the parent. So, you actually create a local copy of the parent's constructor, and then create a new instance of the copy, which returns all constructor methods an properties. By changing the constructor of the child now, it will not affect the parent.
function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
}
function Parent(){}
function Child(){}
inherit(Child, Parent);
// same thing happens here,
Child.prototype.speak = function() {
return 40;
};
var parent = new Parent();
// will throw an error, because speak is undefined
alert( parent.speak() );
The fact that you can change prototype object by pointing another prototype to it is normal JavaScript behavior. JavaScript's primitive values are immutable but objects and arrays aren't. I'll explain it with simple example:
var person = {name: 'greg', age: 20};
>>>person.name; //prints 'greg'
>>>person.age; //prints 20
var a = person;
>>>a.name; //prints 'greg'
a.name = 'peter';
>>>a.name; //prints 'peter'
>>>person.name; //prints 'peter'
//I've changed person.name through a.name. That's why objects in JavaScript are called mutable
Arrays have the same behavior:
var arr = ['first', 'second', 'third'],
newArr = arr;
newArr.pop();
>>>newArr; //prints ['first', 'second']
>>>arr; //prints ['first', 'second']
//Frist array was also changed
Let's look at strings numbers and booleans(primitive data types):
var str = 'hello world',
newStr = str;
>>>str; //prints 'hello world'
>>>newStr; //prints 'hello world'
>>>newStr.toUpperCase(); //prints 'HELLO WORLD'
>>>str; //prints 'hello world'
>>newStr; //prints 'hello world'
//Numbers and booleans have similiar behavior
I had the same issue but i fixed it. Look, i've commented your code, some hints in it should help you:
function Parent(){}
Parent.prototype.say = function () {
return 20;
};
function Child(){
}
/**
*
* The area you should examine i've selected below.
*
*/
//Start BUG
//new say method will not affect the Parent.prototype beacuse it wasn't assigned yet
Child.prototype.say = function () {
return 10;
};
//rewrite Child.prototype and all it's methods with Parent.prototype
inherit(Child, Parent);
//End BUG
function inherit(C, P) {
C.prototype = P.prototype;
}
var parent = new Parent();
var child = new Child();
var child2 = new Child()
alert(child.say(); //20
alert(parent.say()); //20
alert(child2.say()); //20
The problem here is, that instead of copying and changing the Parent.prototype you creating new Child.prototype.say method and right after it you rewriting the whole Child.prototype object through Parent.prototype assignment. Just change their order and it should work fine.