The js call method details the inheritance of of js

  • 2020-03-29 23:48:00
  • OfStack

Call method
Please refer to the
Applied to: Function object
requirements
Version 5.5
Calls a method on an object to replace the current object with another object.

Call ([thisObj [, arg1, arg2, [[, argN]]]]])
parameter
thisObj
Optional. Will be used as the object of the current object.
Arg1, arg2, argN
Optional. A sequence of method parameters will be passed.
instructions
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 its original context to a new object specified by thisObj.

If thisObj parameter is not provided, then the Global object is used as thisObj.

-------------------------------------------------------------------------------------------
At first glance, it's easy to confuse people
Obj1. Method1. Call (obj2, argument1, argument2)
As above, the function of call is to put the obj1 method on obj2 and use it. These are passed in as parameters.

Let me give you a specific example


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

The meaning of this example is to replace sub with add, add.call(sub,3,1) == add(3,1), so the result is: alert(4); // note: the Function in js is actually an object, and the Function name is a reference to the Function object.


Let's do 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);

Notice that call means to put c1's method to execute on c2, which used to have no showNam() method, and now to put c1's showNam() method to execute on c2, so this.name should be class2, and the result is: alert ("class2");

Now, what's interesting is that you can make object a execute method of object b in a way that Java programmers would never dream of. More interestingly, inheritance can be implemented using call


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 to replace this object with a Class1 object. Then all properties and methods of Class1 will be in Class2.

Yes, that's it, that's how javaScript simulates inheritance in object orientation, and 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);
}

Quite simply, multiple inheritance is implemented with two calls
Of course, there are other ways in which js can be inherited, such as using a prototype chain, which is outside the scope of this article, but only to illustrate the use of call
"Call" and "apply" are basically the same thing
The difference is that the second argument to call can be of any type, while the second argument to apply must be an array


Related articles: