(1) Call () method

The call() method calls a function with a specified this value and one or more arguments given separately.

        var sum = {
            add: function () {
                return this.a + The '-' + this.b
            }
        }
        var sum1 = {
            a: 12.b: 34
        }
        var sum2 = {
            a: 56.b: 78
        }
        sum.add.call(sum1);
        console.log(sum.add.call(sum1));//12---34
        console.log(sum.add.call(sum2));//56---78
Copy the code

Call () method with arguments

The call() method accepts an argument: an instance

        var sum = {
            add: function (c, d) {
                return this.a + The '-' + this.b + The '-' + c + The '-' + d
            }
        }
        var sum1 = {
            a: 12.b: 34
        }
        sum.add.call(sum1, 123.456);
        console.log(sum.add.call(sum1, 123.456));/ / 12-34-123-456
Copy the code

(b) apply()

The apply() method calls a function with a given this value and the arguments supplied as an array (or array-like object). The first argument: this refers to the second argument: a data or array-like object that is global if no arguments are passed:

        var x=0;
        function test(){
            alert(this.x);
        }
        var o={};
        o.x=1;
        o.m=test;
        o.m.apply();/ / 0
        o.m.apply(o);/ / 1
Copy the code

(3) of the bind

The bind() method creates a new function, and when bind() is called, this of the new function is specified as the first argument to bind(), and the remaining arguments will be used as arguments to the new function.

        const sum = {
            x: 18.add: function () {
                return this.x; }};const sum1 = sum.add;
        console.log(sum1());// expected output: undefined
        const sum2 = sum1.bind(sum);
        console.log(sum2());// expected output: 18
Copy the code

Difference between apply() and call()

The call method is similar to the apply() method, except that the call() method uses a list of parameters, while the apply() method accepts an array of parameters

Subtle differences between Apply, Call, and bind

Call, bind, and apply all take this as their first argument, and the second, third, and NTH arguments are separated by commas and placed directly after them. All apply parameters must be passed in an array. Bind takes the same arguments as Call, except that it returns a function. Of course, the three parameters are not limited to string type, can be a variety of types, including function, object, etc.!