Ten most confusing things in JavaScript (1)

Source: Internet
Author: User

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. The JavaScript 5 of ECMA has removed global variables. For details, refer to the introduction of JavaScript 5 by ECMA ). 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.


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.