ECMAScript5.1 indicates that the array method concat is a general (generic). This article refuted this conclusion because it is not such an ECMAScript 5.1 specification as mentioned in section 15. 4.4.4:
The Code is as follows:
The concat function is intentionally designed to be generic; it does not require that its this value be an Array object. Therefore, it can be transferred to other types of objects and called as a method.
The code in this article uses [] as Array. prototype shortcut. this is a very common technique, although the readability is almost: you access the Array through an object instance. method on prototype. however, this access method is so fast in modern JavaScript Engines that I suspect that these JavaScript Engines may no longer create array instances. all the examples in this article have tried to run in Firefox and V8.
Let's take a look at whether concat is a common method: if it is a common method, whether the value of this is a real array or a class array object (with the length attribute, can access each element through the index), the returned results of the method should be the same. first, we try to call the concat method on the array:
The Code is as follows:
> ["Hello"]. concat (["world"])
["Hello", "world"]
> []. Concat. call (["hello"], ["world"]) // same as the preceding
["Hello", "world"]
Then, we use a class array object to perform the above join operations. The results should be the same.
The Code is as follows:
> []. Concat. call ({0: "hello", length: 1}, ["world"])
[{'0': 'hello', length: 1}, 'World']
The special variable arguments is also a class array object. The result is still not as expected:
The Code is as follows:
> Function f () {return []. concat. call (arguments, ["world"])}
> F ("hello ")
[{'0': 'hello'}, 'World']
The real common method should be Array. prototype. push:
The Code is as follows:
> Var arrayLike = {0: "hello", length: 1 };
> []. Push. call (arrayLike, "world ")
2
> ArrayLike
{'0': 'hello', '1': 'World', length: 2}
Note: browsers are implemented only according to standards, so there is no bug.