Ten most confusing things in Javascript

Source: Internet
Author: User
Tags bitwise operators

    Javascript is a widely used scripting language for client web development. It 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 dom api, some people once thought that there are many traps in Javascript syntax, and there are also 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, one thing that people have to clarify later 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 is safari tested 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:

 
 
  1. ◦& - and 
  2. ◦| - or 
  3. ◦^ - xor 
  4. ◦~ - not 
  5. ◦>> - signed right shift 
  6. ◦??? - unsigned right shift 
  7. ◦<< - 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 put the following:

    Replace:

    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.

Related Article

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.