A brief introduction and example of variable scope, value-transfer and address of JavaScript basic text

Source: Internet
Author: User
Tags comparison numeric value variable scope

javascript: Declaration of variables
Here are a few ways to declare variables

Copy Code code as follows:


var value;


var value,value1,value2;//declares multiple variables at the same time, but the values of these variables are undefined


var i = 0,j = 0,k=100;//variable declaration, initialized in one.


//If you try to read a nonexistent variable (value) it will be an error! But try to assign a value to a variable that is not declared with Var, javascript


//implicitly declares the amount of change, and the declared variable is global. Details: So you create variables all try to use Var


//variable scope (This problem is also easy to get out, we need to understand)


javascript: Scope of variables
These are the details, and I like the beginner must pay attention to avoid!

Copy Code code as follows:


var golbal = "Golbal"; Global variable


var local = "local";


function Area ()


{


//local variables have higher precedence than global variables


var local = "Arealocal"


//When the function body declares a variable name and a global variable name, JavaScript hides the global variable


var golbal = "Areagolbal";





document.write ("Local is:" +local + "and Golbal are:" + Golbal + "<br/>");


}





area ();


//output: Local is:arealocaland golbal Is:areagolbal


What happens when you define a local variable inside a nested function? Look below:

Copy Code code as follows:


var hope = "Moremoney";


function Createmore ()


{


var hope = "have more money";/local


function Createmoreto ()//nested functions


{


var hope = "have more";/local


document.write ("Createmoreto hope is:" +hope + "<br/>");


//output: Createmoreto hope is:have more for much


}


Createmoreto ();//Call


document.write ("Createmore hope is:" +hope + "<br/>");


//output: Createmore hope is:have More money


}


Createmore (); Call


javascript: Passing values and passing addresses
This is also a more important concept! Don't miss it.

Pass Value Address
Copy The actual copied value, the existence of a different, independent copy. Only a reference to a number is copied. If the value is modified by this new reference, the change is also visible to the original reference.
Passed The independent copy of the value passed to the function has no effect on its change outside the function A reference to a value is passed to a function, and the change is visible if the function modifies the value by passing the reference to it.
Comparison Compares these two opposing values, usually byte by comparison, to determine whether equality Two references are compared to determine whether they are referring to the same numeric value.

javascript: base type and reference type

The basic rule of JavaScript is that the base type operates by passing values, and the reference type is manipulated by the address. (What's the value type, or what's the reference to see my last article)
Pass by value

Copy Code code as follows:


var value = 1;


var copyvalue = value; Assign value to another variable


function AddTotal (total,arg)


{


total+= Arg; Total = total + arg effect equals


}


//Call function, pass two parameters (you may think that this function changes the value of the global variable, in fact, the function is also the opposite copy)


addtotal (Value,copyvalue);


if (value = = 1) copyvalue = 2;


document.write ("Total T" + value + "and Copyvalue tt" + copyvalue+ "<br/>");


//FINAL output: Total 1and copyvalue 2


delivered by address

Copy Code code as follows:


var array = new Array ("JAVASCCCP");


var objarray = array;


function Modifyarray (arr)


{


arr[0] = "JAVASCRIPT";


}


//No function before calling


document.write (array[0] + "<br/>");


//Output JAVASCCCP;


//Call function


Modifyarray (array);


document.write (array[0]+ "<br/>");


//Output uppercase JavaScript


//By modifying the Objarray will be the same effect


objarray[0] = "Frank";


document.write (array[0]+ "<br/>");


//Output Frank;


Summary: Above content hope everybody don't miss, to study behind of knowledge still very helpful!

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.