Faster Array type conversion in js _ javascript skills

Source: Internet
Author: User
Code is always so fun, and Trick keeps on. On that day, Zhang kejun said on twitter that there is another Array conversion method that is shorter than []. slice. call (). Why didn't I share it? I wanted to try it, but I don't have a computer around. Today, when I opened Firebug, I suddenly remembered it. I immediately tried a previous solution. Yes! The Code is as follows:

The Code is as follows:


Function (){
Var arr = Array (arguments );
Alert (arr instanceof Array );
}
A ('B ');


When looking at Baidu Tangram, the most common view is String ('a string'). Unlike in actual usage, it is to force type conversion by adding the literal with an empty string. At that time, I thought that if Array () can be implemented, isn't it better? Of course. Why? Let's take a look at the printed log:

The Code is as follows:


Console. log (Array, [], Array (), new Array ())
// => [Undefined] [] [] []

Console. log (Array ('sofish '), ['sofish'], new Array ('sofish '))
// => ["Sofish"] ["sofish"] ["sofish"] interesting. In fact, like new Array (), new can be omitted and changed to Array (). All of them create an empty Array instance.


As for how to use this method, I have not encountered many situations and cannot describe the status. Type conversion needs to be frequently used, and further research is required. Like the first method, there are at least many popular frameworks. In general, it is used to convert the arguments object of function into an array so that we can do more things on it. If you want to use it, the first method is used for insurance.
Related Article

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.