Small details about JS variable Declaration

Source: Internet
Author: User
Small details about JS variable Declaration
 
 
 
If you try to assign a value to an object that has never been declared, it is implicitly declared as a global object. For example:
 
 
 
(Function (){
 
S = 'abc ';
 
})();
 
Alert (s); // ABC will pop up
 
 
 
If you try to read an undeclared object, JS will report an error. For example:
 
 
 
Alert (a); // No dialog box is displayed, and an error is reported.
 
 
 
This conclusion is written in the rhino book. However, it is interesting that in IE, if you try to read an undeclared object, no error will be reported, and of course no JS execution will continue. In fact, this is really terrible. If you don't execute it, you won't tell you why.
 
 
 
However, this conclusion is strong, so the following statements are common mistakes:
 
 
 
If (! A ){
 
// Do something ....
 
}
 
 
In this way, sometimes the original intention is: if the variable A does not exist, execute the statements in it. However, if A is not defined, an error will be reported in Firefox; no error will be reported in IE, but no subsequent statements will be executed.
 
 
 
So how can we detect whether a variable is defined? A clever way is:
 
 
 
If (! Window. ){
 
// Do something ....
 
}
 
 
 
This method is actually amazing, because an error is reported when you read an undeclared variable, but no error is returned when you read an undeclared attribute. Why does tianxiao JS stipulate such a strange syntax.
 
 
 
However, this method only applies to global variables. If it is a local variable in a function, use typeof to judge it:
 
 
 
If (typeof A = 'undefined '){
 
// Do something ....
 
}
 
 
 
Therefore, the conclusion is that you should not use the variable before it is undefined.
 
 
 
Next, let's look at another example:
 
 
VaR A = "I'm out ";
 
(Function (){
 
Alert ();
 
VaR A = "I'm in ";
 
})();
 
 
 
Okay, ask a question. Run this section.Code, What text will pop up?
 
 
 
Answer 1: "I'm out" is displayed ". Error. JS has no block-level scope. No matter where the VaR statement is, it is defined throughout the function.
 
 
 
Answer 2: "I'm in" is displayed ". Error. Review the above sentence. It is defined only in the entire function body, but only after being assigned a value.
 
 
 
The correct answer is "undefined ". Is it dizzy? Try it on your own.
 
 
 
In short, repeat the previous conclusion: Do not use the variable before it is undefined.
 
 
 
The last question I cannot understand:
 
 
 
Write this code in the HTML document:
 
 
<Script Type= "Text/JavaScript">
 
Window ['A'] ='Hi';
 
</Script>
 
<SCRIPT type ="Text/JavaScript"Src ="Out. js"> </SCRIPT>
 
<SCRIPT type ="Text/JavaScript">
 
Alert ();
 
</Script>
 
 
 
Then write this sentence in out. JS:
 
 
 
If (false ){
VaR A = 'hello ';
 
}
 
 
 
Run ff and IE6 respectively to see what you get.
 
 
 
In ff, "Hi" is displayed, but in IE6, "undefined" is displayed ".
 
 
 
Amazing, right? By syntax, A cannot be undefined in any case. But IE6 will.
 
 
 
This is not the case if you write both statements in the same file.
 
This is not the case if you change window. a In out. js or change the previous one to VaR.
 
If you move var A in out. js out of the IF statement or change the if condition to true, this is not the case.
 
 
 
This bug is very harmful. Therefore, you can only set a good habit for yourself, that is, do not declare variables in conditional statements. Although doing so, it is not prohibited in syntax.

From: Wheat study room

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.