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.