Programming traps (gotcha) refer to unexpected document features in computer systems rather than bugs. These
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.
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.19999999999999998alert(1.14 * 100); //113.99999999999999;)《script》
Math. round () can be used here.
Overload of the plus sign Operator
The plus sign 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 strings or numbers.
Insert a semicolon at the end of a 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(a){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 value of an object.
Typeof Operator
Typeof is a unary operator, and the computation result is often not as expected. Surprisingly, the result of the "null" operation is "object ".
《script》var obj={}; //object created using object literalvar arr=[]; //array created by array literalalert(typeof(obj)); //object - Goodalert(typeof(arr)); //object - Badalert(typeof(null)); //object - Ugly! ;)《script》
It can only find the original type of the object.
False, null, undefined, NaN, Infinity
Although they look similar, they mean different things. 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 a;alert (a); //undefinedalert (1/0); //Infinityalert (0/0); //NaN0/0 == 0/0; //false - a NaN != NaNalert (b); //error《script》
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 occurencevar nospace = "I dont need spaces".replace(/ /g,“_”);alert(nospace); //I_dont_need_spaces《script》
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》
Address of this article: http://www.nowamagic.net/librarys/veda/detail/751,welcome.