JavaScript basics: variable scope, value transfer, and address transfer (2)

Source: Internet
Author: User
Tags define local

Basics
Javascript: Variable Declaration
The following are several ways to declare variables:
Var value;
Var value, value1, value2; // declare multiple variables at the same time, but the values of these variables are undefined
Var I = 0, j = 0, k = 100; // variable declaration, initialization integration.
// If you try to read a nonexistent variable (value), an error is returned! But try to assign a value to a variable that does not use Var declaration, javascript
// The amount of changes is implicitly declared, and the declared variables are still global. Details: use Var whenever possible to create variables.
// Scope of the variable (this problem is also easy to understand)
Javascript: variable scope
These are all details, so be sure to avoid them if you are a beginner like me!
Var golbal = "golbal"; // global variable
Var local = "local ";
Function area ()
{
// The priority of local variables is higher than that of global variables.
Var local = "arealocal"
// When the variable name declared in the function body is the same as the global variable name, javascript hides the global variable.
Var golbal = "areagolbal ";

Document. write ("local is:" + local + "and golbal is:" + golbal + "<br/> ");
}

Area ();
// Output: local is: arealocaland golbal is: areagolbal
How can I define local variables in nested functions? See the following:
Var hope = "moremoney ";
Function createmore ()
{
Var hope = "have more money"; // partial
Function createmoreto () // nested function
{
Var hope = "have more money to much"; // partial
Document. write ("Createmoreto hope is:" + hope + "<br/> ");
// Output: Createmoreto hope is: have more money to much
}
Createmoreto (); // call www.2cto.com
Document. write ("Createmore hope is:" + hope + "<br/> ");
// Output: Createmore hope is: have more money
}
Createmore (); // call
Javascript: transfer value and address
This is also an important concept! Do not miss it.
Value Transfer address
Copy the actually copied value. Different values are copied only by referencing numbers. If you use this
Independent copy. The new reference modifies the value.
References are also visible.

Passed to the function is an independent copy of the value passed to the function is a reference to the value, if the function
The change to it does not affect the modification of the value by passing the reference to it.
Changes are also visible.

Compare the two opposite values. Generally, two references are compared one by one to determine whether the referenced values are
Byte comparison to determine whether the value is equal to the same value.
Javascript: basic type and reference type
The basic rule of javascript is: the basic type is operated by passing values, and the reference type is operated by passing addresses. (For details about the value type or reference, refer to my previous article)
1 pass by value
2 var value = 1;
3 var copyvalue = value; // assign value to another variable
4 function addTotal (total, arg)
5 {
6 total + = arg; // total = total + arg is equivalent
7}
8 // call the function and pass two parameters (You may think that this function has changed the value of the global variable, but not actually, the function is also opposite copy)
9 addTotal (value, copyvalue );
10 if (value = 1) copyvalue = 2;
11 document. write ("total \ t" + value + "and copyvalue \ t" + copyvalue + "<br/> ");
12 // final output: total 1and copyvalue 2
 
1. Pass by address
2 var array = new Array ("invalid CCCP ");
3 var objarray = array;
4 function modifyArray (arr)
5 {
6 arr [0] = "JAVASCRIPT ";
7}
8 // before the function is called
9 document. write (array [0] + "<br/> ");
10 // output Javascccp;
11 // After the function is called
12 modifyArray (array );
13 document. write (array [0] + "<br/> ");
14 // output uppercase JAVASCRIPT
15 // modifying objarray will have the same effect
16 objarray [0] = "Frank ";
17 document. write (array [0] + "<br/> ");
18 // output Frank;
Summary: I hope you will not miss the above content, which is helpful for learning the following knowledge!

 
From ben2012
 

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.