There is no understanding of apply binding in javascript.

I am a novice. Recently, I came across such a code when I was reading a book about javascript . I don"t really understand it. The code for apply binding is as follows:

const concatAll = (array) => {
    let result = []
    for (let value of array) {
        result.push.apply(result, value)
    }
    return result
}

let letters = [["a", "b"], ["c", "d"]]
console.log(concatAll(letters)) // ["a", "b", "c", "d"]

to put it simply, concatAll this function can convert a nested array into an array. What I don"t understand is how line 4 of the code works, and how the method apply works. The previous knowledge about this is also a little confused, not particularly understood. I hope someone can give a detailed answer

.

Thank you!

Mar.12,2021

this is an ingenious use of apply . The first parameter of apply is the this object, and the second parameter is the array collection. The ingenious thing about
here is that she can transform an array into a parameter list ([paramA, paramB, paramC]) to (paramA, paramB, paramC) . Taking advantage of this feature, you can efficiently use her for array merging:
Array.prototype.push !

The

push method does not provide an array of push, but it does provide push (param1,param,. ParamN) so you can also use apply to convert this array. That is, the implementation of your code. I don't know if you understand:)


when you call an existing function, you can specify a this object for it. This refers to the current object, that is, the object that is calling this function. With apply, you can write this method only once and then inherit it in another object without having to repeat it in the new object.

apply is very similar to call (), except in the way parameters are provided. Apply uses an array of parameters instead of a set of parameter lists (original: a named set of parameters). Apply can use array literals such as (array literal), (this, ['eat',' bananas']) or array objects such as fun.apply (this, new Array ('eat',' bananas').

you can also use the arguments object as the argsArray parameter. Arguments is a local variable of a function. It can be used as all unspecified parameters of the called object. In this way, you don't need to know all the parameters of the called object when using the apply function. You can use arguments to pass all the parameters to the called object. The called object is then responsible for processing these parameters.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e391c7-43238.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e391c7-43238.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?