JavaScript is the most popular programming language in the world. It has been labeled as a nightmare by Web developers. Although the real nightmare is actually DOMAPI, A large number of developers and designers have paid more and more attention to this script language to enhance their Web Front-end. However, JavaScript still has a lot of confusing things. 1. It is named in Java, but it is not Java
It was initially called Mocha, and then renamed LiveScript, and finally named JavaScript. According to the historical records, the name of Java is related to the cooperation between Netscape and Sun and serves as the exchange condition, netscape created Java runtime in their popular browsers. It is worth mentioning that this name is almost a joke. You must know that LiveScript and Java have a hostile relationship in client scripts.
In any case, one thing that people have to clarify again and again is that JavaScript has nothing to do with Java.
2. Is Null an object?
Look at this code and it returns the object.
This is really confusing. If null represents a null value, how can it be an object? Simply put, it is an error in the original JavaScript version. This error is even directly borrowed by Microsoft JScript.
3. NaN! = NaN
NaN indicates a non-numeric value, but the problem is that NaN is not equal to anything or even itself.
This is obviously incorrect. In fact, to determine whether a value is actually NaN, you need to use the isNaN () function.
4. Global Variables
The dependency on global variables has always been regarded as the worst part of JavaScript (ECMA's JavaScript 5 has removed global variables, see ECMA's JavaScript 5-Translator's note ). It doesn't matter for simple pages, but for complex pages that contain a large number of JavaScript scripts, it is hard for you to know where a global variable is declared. If you accidentally duplicate several global variables, an error occurs.
5. All browsers tested as Mozilla User-Agent
It must be admitted that, in fact, this is not a JavaScript error, it is intended by various browsers. For example, the following result is obtained when Safari is tested using JavaScript:
Have you noticed the first word?Mozilla/5.0.Why does Safari be detected as Mozilla? Although Safari has fixed this problem later, it still cannot explain why they mislead developers. In fact, you will find that most browsers set their User Agent to Mozilla. The answer is to return to 10 years ago. This is more of a strategy.
The User Agent is a string used to identify the current browser. The first browser Mosaic in the world once marked itself as follows:
This is reasonable, so when Netscape comes out, it retains the Mosaic tradition and adds an encryption method later.
So far, everything has been well maintained until IE3 was released. When IE3 was released, Netscape was in full swing. At that time, many servers and programs had deployed a client detection mechanism to recognize Netscape, although it seems that this is controversial, There was nothing at the time. This Is What IE looks like when introducing their User Agent logo for the first time:
This makes IE very passive, because Netscape has been recognized by many servers, so developers simply want IE to be mistaken for Mozilla, and then add a separate IE label.
Today, almost all browsers follow IE and mark themselves as Mozilla, which is probably a chain reaction.
6. Inconsistent function range
See the following code:
Foo (bar. method) returns different results because the method function is called as a windows Object rather than an object under bar. To solve this problem, we must call bar. method () from the passed anonymous function ().
7. bitwise operators
JavaScript and Java have many similarities, such as bit operations.
- &-And
- |-Or
- ^-Xor
- ~-Not
- >-Signed right shift
- ???-Unsigned right shift
- <-Left shift
Take a look at the first & operator, and use & should be more effective. Because JavaScript is different from Java, JavaScript has no integers and needs to be converted back and forth. Therefore, the conversion operation takes longer.
8. Too many null values
Values such as null, false, and undefined almost share the same meaning, and their differences are confusing.
9. arithmetic problems
Although JavaScript contains a lot of arithmetic operations, you may wish to run the formula below ". 2 +. 4" should be equal to ". 6" or not, but the returned result is "0.6000000000000001 ". There are some minor issues with accessing JavaScript in decimal computation.
Why? To put it simply, because JavaScript uses the IEEE Standard for binary floating point operations, there is no problem with integer calculation.
10. inexplicable code errors
Take a look at the following two sections of code:
They should be the same, but {positions are different, right. However, let's look at the following code:
If we set
Change
This will cause errors. This is because JavaScript has a function that will correct the code that it considers to be wrong, and it will insert ";", the error is generated.