Recently studied the inheritance of JS, looked at the magic days of the article http://www.cnblogs.com/humin/p/4556820.html#3947420, understand that it is best to use the Apply or call method to achieve inheritance.
But for call can be Funciton prototype content copy together, have doubts, after the experiment found is not. Look at the following code, C.G () system is an error is not recognized, do not think it is a function.
function f () {
THIS.A = "a";
this.b = function () {
Console.log ("B");
}
/*
THIS.G = function () {
Console.log ("This was G in F ().");
}
*/
}
f.prototype.g = function () {
Console.log ("This was G in prototype.");
}
function e () {
F.call (this);
}
var c = new E ();
Console.log (C.A); Eject a
C.B (); Eject b
var ff = new F ();
FF.G ();//this is g in prototype.
C.G ();//C.G is not a function
If you want to implement full inheritance of F, you also need to replicate the contents of its prototype chain. Refer to the following code:
function f () {
THIS.A = "a";
this.b = function () {
Console.log ("B");
}
}
f.prototype.g = function () {
Console.log ("This was G in prototype.");
}
function e () {
F.call (this);
F.prototype.call (this);
}
(
function () {
var Super = function () {};
Super.prototype = F.prototype;
E.prototype = new Super ();
}
)();
var c = new E ();
Console.log (C.A); Eject a
C.B (); Eject b
var ff = new F ();
FF.G ();//this is G in prototype
C.G ();//this is G in prototype
The Call.apply method in JavaScript is the content defined for the function itself and cannot be