The Call.apply method in JavaScript is the content defined for the function itself and cannot be

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.