A small mistake leads to thinking of a new method of finally drawing an array to a string

Source: Internet
Author: User

I made a mistake in the project today that is a low-level mistake.

I declare a angle variable in the constructor,

This.angle;

This variable is then given a + = operation in a later function,

This.angle+=90

Finally, after a series of logic, I found that this.angle error NaN, immediately positioned to the constructor, found that the original variable is declared but not initialized assignment

Do not laugh, occasionally will be silly to make some of these low-level mistakes, and this time exposed entirely because I used the + = operation, if the following is a simple = assignment operation, so that the constructor can be uninitialized

At this point, I suddenly want to try if the + = is not the number after the figure, so

this.angle+= "ABC"

Console output UNDEFINEDABC

Originally an uninitialized variable + = A string will get a string of undefined and the string to be spliced together!

This.angle+=true or This.angle+=false

Console output NaN

Originally an uninitialized variable + = a bool value is equivalent to +=1 or +=0 Ah, so finally came to the same as the + = number of Nan Ah!

If you think I'm just finding something like this, you're too young.

this.angle+=[1,2,3, "a"]

Console output Undefined1,2,3,a

And then I found the array into a string.

Then it occurred to me that there was a simple way to convert a number to a string.

var num=1;

num+= ""

Get num= "1"

If we put an array of + = ""

[1,2,3,a]+= ""

Get 1,2,3,a

Then use replace to kill, and then successfully convert the array to a string

So, the array to the string I found another way, although it may have been discovered, or this method is stupid, but I finally found the second method

var arr=[1,2,3,5];

Arr= (arr+ ""). Replace (/,/g, "");

Finally get 1235

The most commonly used array-to-string method is

Arr=arr.join ("");

Of course, the most common way of looking at it seems to be better than the above, but that's not the point.

The point is I found a way to have wood!!

So sometimes make some small mistakes do not easily take a stroke, calm down to think about, think more, maybe you will have a new harvest OH

Ps:

Attach a string to the array of high-force lattice method, is also previously inadvertently found yo

The general approach is

Str.split ("");

The high-force lattice method is

Str=array.prototype.slice.call (str); Call to apply can also

A small mistake leads to thinking of a new method of finally drawing an array to a string

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.