prototype - javascript concat using call method -
i'm trying concat 2 arrays in javascript using concat() method. i'm wondering difference between these 2 cases
var = [1, 2, 3]; console.log(array.prototype.concat.call(a, [4, 5, 6])); // result: [1, 2, 3, 4, 5, 6] console.log(array.prototype.concat(a, [4, 5, 6])); // result: [1, 2, 3, 4, 5, 6] i thought using call method, passing in this object first argument, change array a. didn't.
array.prototype.concat.call(a, [4, 5, 6])
is equivalent a.concat([4, 5, 6]) (assuming a.concat === array.prototype.concat, case in example a instanceof array).
array.prototype.concat(a, [4, 5, 6])
is equivalent array.prototype.concat.call(array.prototype, a, [4, 5, 6]).
given array.prototype empty array, both yield same result. [].concat(a, [4, 5, 6]) or array.prototype.concat.call([], a, [4, 5, 6]) well.
Comments
Post a Comment