Scope 2-js Learning note 2015-6-10 (54th day)

Source: Internet
Author: User

parameter, also can be understood as an expression, the parameter is used when the equivalent assignment

var a=1;

function Fn1 (a) {//Here the parameter a equals: var a = 1; A is undefined and becomes 1, here the variable is a local variable, with the outside Var a=1;

alert (a); 1 Here is the local variable;

a=2;

}

FN1 (a);

alert (a); 1 because the A in here is still access to global variables;

To get the value inside a function:

Method one: Through the scope chain, reverse access; global variables;

var str = ';
function fn1 () {

var a= ' Awuawu ~ ';
str = A;

}
FN1 ();
alert (str); At this point the value of STR is a;

Method Two: pass through the parameter;

function fn2 () {

var a= ' free ~ ';
Fn3 (a);

}
FN2 ();
function Fn3 (a) {//Here is not a above, can also be b,c ... Used to receive the pass of a function
alert (a);
}

Second: if judgment, for loop is not scope, and function is not the same, but use time to be particularly careful;

First parse, then execute, is the scope of the flag; so the function is a scope; but the if,for is not a scope;

For compatibility reasons, when you want to define global variables, or global functions, put them outside the if,for, etc.

The FOR function does not use the defined variable i directly, otherwise there will be a hidden danger (we should be very careful when using the For loop, put in a different position, may be different values)

For example:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Untitled Document</title>6 7 8 9 </Head>Ten  One <Body> A  - <inputtype= "button"value= "Button 1"> - <inputtype= "button"value= "button 2"> the <inputtype= "button"value= "button 3"> -  - <Script> -  + varabtn=document.getElementsByTagName ('input'); -  +  for( varI=0; I<Abtn.length;i++){ A  at Abtn[i].onclick= function(){ -        -      //alert (i);//return 3; -        //abtn[i].style.background = ' blue ';//Click the button at this time, there will be no response and error, because at this time I has been three times since the increase, became 3; Abtn[3] does not exist at all; -       for( varI=0; I<abtn.length; I++){ - Abtn[i].style.background= 'Blue'; in      } -  to   } +  - } the </Script> * </Body> $ </HTML>

Scope 2-js Learning note 2015-6-10 (54th day)

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.