Home > Web Front-end > JS Tutorial > Shorter Array type conversion in js_javascript skills

Shorter Array type conversion in js_javascript skills

WBOY
Release: 2016-05-16 18:00:06
Original
1085 people have browsed it

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:

Copy code 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:
Copy the code 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(), 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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template