Home >Web Front-end >JS Tutorial >Instructions for using the javaScript call function_javascript skills

Instructions for using the javaScript call function_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:30:001124browse

call method
See
Applies to: Function objects
Requires
Version 5.5
Call a method on 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 no thisObj parameter is provided, the Global object is used as thisObj.
-------------------------------------------------- -----------------------------------------------
At first glance , it is easy to confuse people, let’s give some simple explanations first
obj1.method1.call(obj2,argument1,argument2)
As above, the function of call is to put the method of obj1 on obj2 and use it later. argument1.. These are passed in as parameters.
Give a specific example

Copy the code The code is as follows:

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

In this example, it means to replace sub with add, 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
Copy the code The code is as follows:

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. Now it is to put the method of c1 The showNam() method is placed on c2 for execution, so this.name should be class2, and the execution result is: alert ("class2");
What about, you think it’s interesting, you can let the a object execute the b object This method is something that Java programmers dare not think of. What’s more interesting is that you can use call to implement inheritance
Copy the code The code is as follows:

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

This way Class2 inherits Class1, Class1.call(this) means to use the Class1 object instead of this object, then Class2 does not 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 inheritance in object-oriented and can also implement multiple inheritance.
Copy code The code is as follows:

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, using two calls to achieve multiple inheritance
Of course, there are other methods of js inheritance, such as using prototype chain, This does not belong to the scope of this article, it is just to explain the usage of call
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 , and the second parameter of apply must be an array, or it can be arguments
and callee, caller. This is different from the usage of call. I will talk about it next time, haha.
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