Home >Web Front-end >JS Tutorial >js call usage
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.
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, so let’s make some simple explanations
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);
The meaning in this example is 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.
Let’s 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. 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");
What do 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");
so that Class2 inherits Class1. Class1.call(this) means using the Class1 object instead of this object, so won't Class2 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 works To simulate inheritance in object-oriented, multiple inheritance can also be implemented.
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, which is not within the scope of this article, but is just here 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 the second parameter of apply must be Array
Recommended tutorial: "JS Tutorial"
The above is the detailed content of js call usage. For more information, please follow other related articles on the PHP Chinese website!