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