I was at a sharing session and wanted to try it out, but I didn’t have a computer around me. The moment I opened Firebug today, I suddenly remembered this. I immediately tried a plan I had thought of before. Can! The code is as follows:
function a(){
var arr = Array(arguments);
alert(arr instanceof Array);
}
a('b');
When watching Baidu Tangram, the most common What we get is String('a string'), which is different from the actual local usage, which uses addition to an empty string literal to force type conversion. At that time, I thought that if Array() could be implemented, wouldn’t it be more fun? certainly. And why? You can take a look at this printed log:
console.log (Array,[],Array(),new Array())
// => [undefined] [] [] []
console.log(Array('sofish'), [ 'sofish'], new Array('sofish'))
// => ["sofish"] ["sofish"] ["sofish"]Interesting. In fact, like new Array(), you can omit new and change it to Array(), which creates an empty array instance.
As for which method is better, I haven’t encountered many situations, so I can’t explain the situation. We need to use type conversion frequently, and we need to do more in-depth research. The first method is at least the method used by many popular frameworks. Generally, it is used to convert the arguments object of a function into an array so that we can do more with it. If you want to use it, use the first method of insurance.