Home  >  Article  >  Web Front-end  >  How to use the call method of javascript

How to use the call method of javascript

青灯夜游
青灯夜游Original
2021-04-12 14:59:499700browse

In JavaScript, the call method can be used to call a method instead of an object. It can change the object context of a function from initialization to a new object, that is, the original object in the brackets is changed to call() the previous object.

How to use the call method of javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Usage of call() method in js

1.call method

Syntax: call(thisobj,[argq,arg2])

Definition: Call a method of an object and replace the current object with another object

Description:

The call method can be used to call a method instead of an object. The call method can change the object context of a function from initialization to a new object, that is, the original object in the brackets is changed to the object before call(), which is ready to use. thisobj replaces the thing before call, and finally uses thisobj object to execute the method before call.

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

Just like

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>js中call方法的使用</title>
</head>
 
<body>
<p id="id1">新年</p>
</body>
 
</html>
 
<script>
    function add(a,b){
        alert(a+b);
    }
    function sub(a,b){
        alert(a-b);
    }
    document.getElementById("id1").onclick = function(){
        add.call(sub,3,1);
    }
 
</script>

add replaces sub , the most summarized output is

The meaning in this example is to use add to replace sub, 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.

[Recommended learning: javascript advanced tutorial]

2.apply method

Syntax: apply(thisobj ,[argArray])

Definition: Apply a method of a certain object and replace the current object with another object

Description:

If argArray is not a A valid array or not an arguments object, a TypeError will result.
If neither argArray nor thisObj is provided, the Global object will be used as thisObj and no parameters can be passed.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>js中call方法的使用</title>
</head>
 
<body>
<p id="id1">新年</p>
</body>
 
</html>
 
<script>
    function Animal(){
        this.name = "Animal";
        this.showName=function(){
            alert(this.name);
        }
    }
    function Cat(){
        this.name = "Cat";
    }
 
    document.getElementById("id1").onclick = function(){
        var animal = new Animal();
        var cat = new Cat();
        // 通过call或apply方法,将原本属于Animal对象的showName方法交给对象call来使用了
        animal.showName.apply(cat,[]);
        animal.showName.call(cat,",");
 
    }
 
 
</script>

Note that the parameters of apply are different from those of the call method. apply is an array

3.call implements js inheritance

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to use the call method of javascript. For more information, please follow other related articles on the PHP Chinese website!

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