When looking for some JavaScript Information, I read some blog posts, one of which is a blog http://blog.csdn.net/leadzen/article/details/3899392 about the analysis of null and undefined, which summarizes some, comments Let me think a lot, I personally think it is an article with a lot of misunderstanding, but the visits and replies are basically good. I even suspected myself (I think my JavaScript knowledge is far from good enough ). when I look for some technical articles in various forums in China,
It seems that more than 90% of people have serious deviations in understanding JavaScript (when looking for some materials, I am not sure to read some articles on domestic forums ). I also want to write something that will make my thoughts clearer and help some people.
First, add some instructions to the term JavaScript itself, see the http://www.w3school.com.cn/js/pro_js_history.asp (w3school.com.cn content is basically trusted, basically is the foreign w3school translation, the content inside is relatively authoritative ). all ecmascript used later in this article represents the Javascript that we usually call.
For the previous blog posts, make some instructions (we suggest you open a link to view the blog posts, and do not post them due to the format and length)
1. for null and undefined, The ecmascript Specification defines the data types in section 6, null, undefined, Boolean, number, string, and object. null and undefined are unique values of the type null and undefined respectively. <see ecmascript
Language Specification chapter 4th, Chapter 8th>. For comparison between null and undefined, the language specification itself indicates: when comparing X = Y, ifX is null and Y is undefined.
Or X is undefined and Y is nullReturns true. And! = The symbol is relative to = (strict equality), and undefined = NULL
Yes false is returned. <for details about comparison rules, see section 11.9 of ecmascript language specification.>.
Therefore, there is no surprise with the comparison results between the two.
2. Operations on null and undefined using the typeof Operator
If the type of the value is undefined, the system returns "undefined" by running the typeof operation on the null type, and returns "object". If the type of the other type is null, the return value of the typeof operation is shown in the following table.
Table 20-typeof operator results
Type of Val |
Result |
Undefined |
"undefined" |
Null |
"object" |
Boolean |
"boolean" |
Number |
"number" |
String |
"string" |
Object (native and does not implement [[Call]) |
"object" |
Object (native or host and does implement [[Call]) |
"function" |
Object (host and does not implement [[Call]) |
Implementation-defined partition T may not be"undefined" ,"boolean" ,"number ", Or"string". |
For more information about the operation rules of typeof, see <ecmascript language specification 11.4.3>.
3. Convert null and undefined to other types
3.1 To Boolean Type
Undefined --> false
Null --> false
Number (+ 0,-0, Nan) returns false, others return true
String type. If it is null, false is returned. Otherwise, true is returned.
For more information, see <ecmascript language specification Chapter 9>.
3.2 To number type
Undefine will return Nan
Null returns 0.
4. About ecmascript (JavaScript) and HTML
Previously mentioned in the blog post, the blogger mentioned that undefined is the property of window. This sentence should be correct, but it is definitely not inevitable! Ecmascript itself is only a language. Although it is basically used as a Browser Scripting Language, it should be unrelated to the browser. The window object is HTML.
A root object (Global Object) of the DOM model. The window object has the undefined attribute only because of the implementation of the browser JavaScript engine. It has nothing to do with ecasloud itself.
The window object has no contact with undefined.
The above are some analyses of this and undefined behaviors. We mainly see blog posts.
Reference (ecmascript specifications ):
Http://www.ecma-international.org/ecma-262/5.1/
The PDF version can be downloaded from my csdn resources or from the ECMA website.