Home > Web Front-end > JS Tutorial > body text

Instructions for using the javascript call method_Basic knowledge

WBOY
Release: 2016-05-16 18:36:59
Original
875 people have browsed it

Let’s take a look at the official explanation first:
call method
Please refer to
Applies to: Function object
Requires
Version 5.5
Call a method of an object to replace the current one with another object 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 be confused, so 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

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 showNam() method of c1. ) method is put on c2 for execution, so this.name should be class2, and the execution result is: alert ("class2");
What about, I think it’s interesting, you can let object a execute the method of object b, This 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 ways to inherit js, such as using prototype chains, this does not belong The scope of this article 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, while apply The second parameter 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.
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