JS prototype, apply, call

Source: Internet
Author: User

1: The native chain: prototype

A son can inherit the attributes of his father, or he can feel that the genetic gene is not good for his own property, (but not to change his father's attributes).

See Example:

function farther () {
THIS.name = "Jack",
This.sex = "Man",
This.age = 18;
THIS.CBO = function () {
Console.log (this.name);
}
}
Inherited
Farther.prototype = {
Son:function () {
THIS.name = "Jack-two";
Use your own name and the sex of the parent class
Console.log (this.name + "\ T" + this.sex);
Change the method of the parent class.
THIS.CBO = function () {
Console.log ("CBO change.");
};
THIS.CBO ();
}
}
New farther (). CBO (); Output jack
New farther (). Son (); Output jack and Jack-two plus man plus cbo change.
New farther (). CBO (); The output value is still jack; subclasses cannot change parent class properties

2:call (list) and apply (Arry):

Usage 1: Object 1. Method. Call (object 2),

The method of object 1 uses the properties of Object 2,

Usage 2: Object 1.call (object 2); Object 2. Method of Object 1,

The method that uses object 1 in Object 2.

The usage is different, the effect is the same, a missing method, a missing attribute.

Why put call and apply together? Because they have the same effect, except for the parameters.

function nameshowing (sex, age) {
This.showname = function () {
Console.log (this.name + "\ T" + sex + "\ T" + age);
}
}
function person (name) {
THIS.YOURCC = function () {
Console.log (name)
}
THIS.name = name;
};
Instantiating an Object
var nameshowing = new nameshowing ();
var jeremy = new Person ("Jeremy")
Replace this to point to Jeremy
Nameshowing.apply (Jeremy, ["Girl", 18]);
Jeremy.showname ();
Of course, you can use call (this) to federate 2 objects
function Cexent (name,sex,age) {
Nameshowing.call (This,sex,age);
Person.call (This,name);
}
This will execute Fobj
var C2 = new Cexent ("Jack", "man", 18);
Call the MyName method of nameshowing
C2.showname ();
C2.YOURCC ("My Name is Jack"); Output jack, because it was instantiated with Jack.

JS prototype, apply, call

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.