I can't talk about the translation of "Xin \ YA \ da". I hope I can make a mistake in the article.
Programming traps (gotcha) refer to unexpected document features in computer systems rather than bugs. these traps keep beginners away from javascript programming. in my opinion, because all browsers can run javascript, it is one of the most widely used languages, but it is also researched by at least people. let's start with a basic example.
1. Floating Point Operation
This may be the main reason to drop people who are not familiar with javascript and are ready to execute some mathematical operations.
- <Script>
- Alert (0.02/0.1); // 0.19999999999999998
- Alert (1.14*100); // 113.99999999999999
- </Script>
Math. round () can be used here.
2. overload of the plus sign Operator
"+" The plus operator can perform arithmetic operations and connect strings. It is very convenient to use it correctly. Let's take a look.
- <Script>
- Var msg, one = "1 ";
- Msg = 2 + "1"; // msg = "21"
- Msg = 2 + one; // msg = "21"
- Msg = 1 + 1 + 1 + "musketeers"; // msg = "3 musketeers"
- Msg = "Bond" + 0 + 0 + 7; // msg = "Bond 007"
- </Script>
These operations are performed from left to right. type conversion is based on the strings or numbers.
3. Insert a semicolon at the end of the row
Javascript automatically inserts a semicolon ";" at the end of a row. Let's take a look at this situation in a simple example.
- <Script>
- Function returnSame (){
- Return // Inserts semi-colon to convert to return;
- A // a becomes a;-Unreachable
- }
- Alert (returnSame (2); // Output is undefined
- </Script>
This magic semicolon can make things more complex when creating an object or using the object value.
4. typeof Operator
Typeof is a unary operator, and the computation result is often not as expected. What is surprising is that the computation result for "null" is "object"
- <Script>
- Var obj = {}; // object created using object literal
- Var arr = []; // array created by array literal
- Alert (typeof (obj); // object-Good
- Alert (typeof (arr); // object-Bad
- Alert (typeof (null); // object-uugly!
- </Script>
It can only find the original type of the object.
5. false, null, undefined, NaN, Infinity
Although they look similar, they do not mean anything. javascript has three basic data types: numbers, strings, and boolean. In addition, there are two unimportant data types: undefine and null ". according to the "=" operator, null and undefine are equal.
- <Script>
- Var;
- Alert (a); // undefined
- Alert (1/0); // Infinity
- Alert (0/0); // NaN
- 0/0 = 0/0; // false-a NaN! = NaN
- Alert (B); // error
- </Script>
6. The string must replace the first matched character.
Unlike PHP or other programming languages, by default, the character replacement of javascript only replaces the first matched character.
- <Script>
- Var nospace = "I dont need spaces". replace ("","_");
- Alert (nospace); // I _dont need spaces-Only first occurence
- Var nospace = "I dont need spaces". replace (// g ,"_");
- Alert (nospace); // I _dont_need_spaces
- </Script>
7. parseInt Function
ParseInt is used to convert a string to the integer type. this function can input two parameters. The second parameter specifies the number of hexadecimal parameters. in decimal format, 10 is used. if the hexadecimal value is not specified, the parseInt function will try to find the appropriate hexadecimal value. if so, the string starting with 0 will be converted to octal.
- <Script>
- Var str = "017 ";
- Var strInt = parseInt (str); // strInt = 15
- Var strInt = parseInt (str, 10); // strInt = 17
- </Script>