JavaScript - apply() method

revision:


Category: function.prototype

The apply() method calls the specified function with a given "this" value, and "arguments" provided as an array (or an array-like object).

It does the same as the call() method. The difference is that call() accepts an argument list, but apply() accepts an array of arguments.

Syntax :

    apply(thisArg)
    apply(thisArg, argsArray)

Parameters

thisArg : the value of "this" provided for the call to "func". If the function is not in strict mode, "null" and "undefined" will be replaced with the global object, and primitive values will be converted to objects.

argsArray : an array-like object, specifying the arguments with which "func" should be called, or "null" or "undefined" if no arguments should be provided to the function.

Examples

    const array = ["a", "b"];
    const elements = [0, 1, 2];
    array.push.apply(array, elements);
    console.info(array); // ["a", "b", 0, 1, 2]

    const numbers = [5, 6, 2, 3, 7];
    const max = Math.max.apply(null, numbers);
    console.log(max);
    // Expected output: 7
    const min = Math.min.apply(null, numbers);
    console.log(min);
    // Expected output: 2

Practical examples

example: calculate a total

code:
                <div classs="spec">
                    <p id="two"></p>
                </div>
                <script>
                    var obj = { num: 3 };
                    function add(a, b){
                        return this.num + a + b;
                    }
                    document.getElementById("two").innerHTML = add.apply(obj, [3, 5]);
                </script>
           

example: use apply() to append an array to another array.

code:
                    <div class=spec>
                        <p id="seven"></p>
                    </div>
                    <script>
                        const array = ['a', 'b'];
                        const elements = [0, 1, 2];
                        array.push.apply(array, elements);
                        document.getElementById("seven").innerHTML = array; // ["a", "b", 0, 1, 2]
                    </script>
                

example: use apply() to chain object constructors.

code:
                <div class="spec">
                    <p id="eight"></p>
                </div>
                <script>
                    //Create constructor method for array of args
                    Function.prototype.construct = function(aArgs) {
                    var oNew = Object.create(this.prototype);
                    this.apply(oNew, aArgs);
                    return oNew;
                    };
                    function Person(name, gender) {
                        this.name = name;
                        this.gender = gender;
                    }
                    var args = ["Helen", "F"];
                    var person = Person.construct(args);
                    document.getElementById("eight").innerHTML = Object.values(person);
                </script>