Basic JavaScript: A Brief Introduction to variable scope, value transfer, and address transfer and the example _ javascript skills

Source: Internet
Author: User
Tags define local variable scope
This article introduces the scope, value passing, and address passing of variables. If you need them, you can refer to javascript: Variable declaration.
The following are several ways to declare variables:

The Code is as follows:


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!

The Code is as follows:


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 +"
");
}

Area ();
// Output: local is: arealocaland golbal is: areagolbal


How can I define local variables in nested functions? See the following:

The Code is as follows:


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 +"
");
// Output: Createmoreto hope is: have more money to much
}
Createmoreto (); // call
Document. write ("Createmore hope is:" + hope +"
");
// Output: Createmore hope is: have more money
}
Createmore (); // call


Javascript: transfer value and address
This is also an important concept! Do not miss it.

Pass Value Address Transfer
Copy The actual copy value has different and independent copies. Only a reference to a number is copied. If a value is modified using this new reference, this change is also visible to the original reference.
Transfer The independent copy of the value passed to the function has no effect on its changes outside the function. The value passed to the function is a reference to the value. If the function modifies the value through the reference passed to it, this change is also visible.
Comparison Compare these two opposite values, usually byte comparison, to determine whether it is equal Two references are compared to determine whether they reference 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)
Pass by value

The Code is as follows:


Var value = 1;
Var copyvalue = value; // assign value to another variable
Function addTotal (total, arg)
{
Total + = arg; // total = total + arg equivalent
}
// Call the function and pass two parameters (You may think that this function has changed the value of the global variable. In fact, this function is not used, and the function is also opposite copy)
AddTotal (value, copyvalue );
If (value = 1) copyvalue = 2;
Document. write ("total \ t" + value + "and copyvalue \ t" + copyvalue +"
");
// Final output: total 1and copyvalue 2


Pass by address

The Code is as follows:


Var array = new Array ("Javascccp ");
Var objarray = array;
Function modifyArray (arr)
{
Arr [0] = "JAVASCRIPT ";
}
// Before the function is called
Document. write (array [0] +"
");
// Output Javascccp;
// After the function is called
ModifyArray (array );
Document. write (array [0] +"
");
// Output uppercase JAVASCRIPT
// Modifying objarray will have the same effect
Objarray [0] = "Frank ";
Document. write (array [0] +"
");
// Output Frank;


Summary: I hope you will not miss the above content, which is helpful for learning the following knowledge!

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.