I can’t talk about the translation when it comes to "faith\yellowness\dat". I hope I can’t make the meaning of the article wrong.
The traps of programming (getcha) 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 the least studied. Let's start with a basic example.
1. Floating point operation
This may be the main reason to frustrate some people who are not familiar with javascript and are ready to perform some math operations.
- <script>
- alert(0.02 / 0.1); //0.19999999999999998
- alert(1.14 * 100); //113.99999999999999 ;)
- </script>
() can come in handy here.
2. Overloading of plus operator
The "+" plus operator can do arithmetic operations and string connections. 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>
The above behavior is because these operations are performed from left to right. The conversion of types is based on the string or number in it.
3. Insert semicolons at the end of the line
JavaScript automatically inserts semicolon ";" at the end of the line, let's see how this happens 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 magical semicolon can make things more complicated when creating objects or using their values.
Operator
typeof is a unary operator, and the operation result is often not as expected. Surprisingly, the operation 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 - Ugly! ;)
- </script>
It can only find the original type of the object.
5. false, null, undefined, NaN, Infinity
Although they look similar, they representThe meaning of obstruction is not. JavaScript has three basic data types: numbers, strings and boolean. In addition, there are two unimportant data types: "undefined" and "null". According to the "==" operator, null and undefined are equal.
- <script>
- var a;
- 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 only replaces the first matching character
Unlike PHP or other programming languages, by default, javascript's character replacement only replaces the first matching character that appears.
- <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 an integer type. This function can pass in two parameters, the second parameter specifies how much of the binary. Here the decimal is specified with 10. If no binary is specified, the parseInt function will try to find the appropriate binary. If this is the case, a 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>