Home >Web Front-end >JS Tutorial >Detailed introduction to js call method (inheritance of js)_Basic knowledge

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

WBOY
WBOYOriginal
2016-05-16 17:14:34965browse

call method
See
Applies to: Function object
Requires
Version 5.5
Calls 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.
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 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);


What this example means 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.


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");

How about it, you think it’s interesting, you can let object a execute the method of object b, which 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");


In this way, Class2 inherits Class1. Class1.call(this) means using the Class1 object instead of this object. Then there is no such thing in Class2 All properties and methods of Class1, the c2 object can directly call the methods and properties of Class1, and 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, Multiple inheritance is achieved by using two calls
Of course, there are other methods of js inheritance, such as using prototype chains. This is not within the scope of this article. It is just to explain the usage of call
I mentioned call, and of course There is apply, these two methods basically have the same meaning
The difference is that the second parameter of call can be of any type, while the second parameter of apply must be an array
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