Array. prototype. concat is not a common method to refute [translation]

Source: Internet
Author: User

ECMAScript 5.1 Specification section 15. 4.4.4 states:

Copy codeThe Code is as follows: The concat function is intentionally designed to be generic; it does not require that its this value must 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:

Copy codeThe 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.Copy codeThe 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:

Copy codeThe 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:Copy codeCode:> 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.