Home  >  Article  >  Web Front-end  >  Detailed explanation of call method in js

Detailed explanation of call method in js

小云云
小云云Original
2018-03-22 16:16:093504browse

This article mainly shares with you the detailed explanation of the call method in js, hoping to help everyone.

call method
See
Applies to: Function object
Requires
Version 5.5
Call a method of an object, replacing 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.
Description
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.
At first glance, it is easy to be confused. Let’s make some simple explanations first
obj1.method1.call(obj2,argument1,argument2)
As above, the function of call is to put the method of obj1 Use it on obj2, and the following argument1..are passed in as parameters.
Give a specific example

function add(a,b) 
{ 
    alert(a+b); 
} 
function sub(a,b) 
{ 
    alert(a-b); 
} 
add.call(sub,3,1);


This example means using add to replace sub, add.call(sub,3,1) == add(3,1), so The running result is: alert(4); // Note: functions in js are actually objects, and the function name is a reference to the Function object.
Look at a slightly more complicated example

function Class1() 
{ 
    this.name = "class1"; 
    this.showNam = function() 
    { 
        alert(this.name); 
    } 
} 
function Class2() 
{ 
    this.name = "class2"; 
} 
var c1 = new Class1(); 
var c2 = new Class2(); 
c1.showNam.call(c2);

Note that call means to put the method of c1 on c2 for execution. Originally, c2 did not have a showNam() method, but now it is to put the showNam() method of c1. The method is placed on c2 for execution, so this.name should be class2, and the execution result is: alert ("class2");
How about it, you think it is interesting, you can let object a execute the method of object b, this It is something that java programmers dare not think of. What’s more interesting is that you can use call to implement inheritance.

function Class1() 
{ 
    this.showTxt = function(txt) 
    { 
        alert(txt); 
    } 
} 
function Class2() 
{ 
    Class1.call(this); 
} 
var c2 = new Class2(); 
c2.showTxt("cc");


In this way, Class2 inherits Class1. Class1.call(this) means using the Class1 object instead of this object. Then Class2 won’t Do you have all the properties and methods of Class1? The c2 object can directly call the methods and properties of Class1. The execution result is: alert ("cc");
Yes, that's it. This is how javaScript simulates oriented Inheritance in objects can also achieve multiple inheritance.

function Class10() 
{ 
    this.showSub = function(a,b) 
    { 
        alert(a-b); 
    } 
} 
function Class11() 
{ 
    this.showAdd = function(a,b) 
    { 
        alert(a+b); 
    } 
} 
function Class2() 
{ 
    Class10.call(this); 
    Class11.call(this); 
}

It’s very simple. Use two calls to achieve multiple inheritance.
Of course, there are other methods of js inheritance, such as using the prototype chain. This is not within the scope of this article. I just explain the call here. Usage
I mentioned call, and of course apply. These two methods basically mean the same thing.
The difference is that the second parameter of call can be of any type, while the second parameter of apply must be an array.

Related recommendations:

Detailed explanation of the instance usage of function function call method in Javascript

php __call method usage instructions

Detailed introduction to js call method (inheritance of js)_Basic knowledge

The above is the detailed content of Detailed explanation of call method in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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