Home > Web Front-end > JS Tutorial > Summary of several usages of inheritance in js (apply, call, prototype)_javascript skills

Summary of several usages of inheritance in js (apply, call, prototype)_javascript skills

WBOY
Release: 2016-05-16 17:07:01
Original
1190 people have browsed it

1. Object inheritance in js

There are three inheritance methods in js

1.js prototype implementation inheritance

Copy code The code is as follows:







2. Constructor implementation inheritance
Copy code The code is as follows:








3.call, apply implements inheritance



Copy code

The code is as follows:





How to use apply (detailed introduction)



Both call and apply in js can be inherited. The only parameter difference is that the apply corresponding to func.call(func1,var1,var2,var3) is written as: func.apply(func1,[var1,var2,var3] ).

Explanation of call in the JS manual:

Copy code The code is as follows:

< ;SPAN style="FONT-SIZE: 18px">call method
Call a method of an object to replace the current object with another object.

call([thisObj[,arg1[, arg2[, [,.argN]]]]]) Parameters thisObj Optional. The object that will be used as the current object.
arg1, arg2, , argN
Optional. A sequence of method parameters will be passed.

Explanation The
call method can be used to call a method instead of another object. The call method changes the object context of a function from the initial context to the new object specified by thisObj.

If the thisObj parameter is not provided, the Global object is used as thisObj.


To put it simply, the function of these two functions is actually to change the internal pointer of the object, that is, to change the content pointed to by this of the object. This is sometimes useful in object-oriented js programming. Let's take apply as an example to talk about the important role of these two functions in js. For example:




Copy code


The code is as follows:

function Person(name,age){ //Define a class
this.name=name; //Name
this. age=age; //Age
this.sayhello=function(){alert(this.name)};
}
function Print(){ //Display class attributes
this.funcName ="Print";
this.show=function(){
var msg=[];
for(var key in this){
if(typeof(this[key])!= "function"){
                                                                          g.join(" "));
};
}
function Student(name,age,grade,school){ //Student class
Person.apply(this,arguments);//Superior to call Place
Print.apply(this,arguments);
this.grade=grade; //Grade
this.school=school; //School
}
var p1=new Person( "Bu Kaihua",80);
p1.sayhello();
var s1=new Student("Bai Yunfei",40,9,"Yuelu Academy");
s1.show();
s1.sayhello();
alert(s1.funcName);



In addition, Function.apply() plays a prominent role in improving program performance:
Let’s start with the Math.max() function. Math.max can be followed by any number of parameters, and finally returns the maximum value among all parameters.
For example



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template