JavaScript variable Scope!

Source: Internet
Author: User
Tags getcolor variable scope

Basic types and reference types

A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values
When you assign a value to a variable, the JavaScript parser first determines whether it is a basic type or a reference type, and the base data type can manipulate the value stored in the variable directly, while the value of the reference data type is the object that is held in memory, in which the object is actually manipulating the object's reference rather than the actual object

Assigning values to variables

If you copy the value of the base data type from one variable to another, a new value is created on the variable object, and then the value is copied to the position of the new variable, which is a good understanding, see the example:javascript

var num1 = 5;

var num2 = num1;


This is the basic data type, and the reference type? In fact, the value on the original variable is copied to the new variable, but the copy is actually a pointer to the original variable, and this pointer points to an object stored in the heap. When the copy is complete, the two variables point to the same object in the heap, so changing one of the values will have an effect on the other. Has the following code
javascript

var obj1 = new Object();

var obj2 = obj1;

obj1.name = "Nicholas";

alert(obj2.name); //"Nicholas"

Passing parameters

In JavaScript, the arguments are passed in 值类型来传递 , even if you pass in a reference typejavascript

function setName(obj) {

obj.name = "Nicholas";

}

var person = new Object();

setName(person);

alert(person.name); //"Nicholas"
The return result of the above code looks like the argument is the pass of the reference type because the start Person object has no properties, and after the SetName method is called, the parameter obj is added with the name parameter. Then the person outside the print person.name unexpectedly is a value, which is very obvious the effect of the reference delivery. But do not be fooled by this phenomenon, JavaScript when passing parameters of the reference type, as long as this parameter does not change, then the reference type to deal with, but only the change effect is completely different.
javascript function setName(obj) {

obj.name = "Nicholas"; obj = new Object();

obj.name = "Greg"; }

var person = new Object(); s

etName(person);

alert(person.name); //"Nicholas"
The above example can see the difference, if it is really a reference pass, then obj re-assignment, and added the name parameter, the last person to print should be the name of Greg, but the result is the original value, can only show that JavaScript dug a big hole here, very confusing people, But just figure this out.

Execution Environment and scope

Execution Environment (execution context) defines the other data that a function or variable has access to, determines their behavior, and each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object

The global execution environment is the outermost execution environment, in the Web browser, is actually the window object, therefore, all global variables and functions are created as window properties and methods, an execution environment after all the code is executed, the environment is destroyed, The variables and function definitions stored therein are also destroyed (the global execution environment is not destroyed until the application exits and the browser shuts down)

each function has its own execution environment , when the program executes to a function, the function of the environment is created, into the current process of the program, and after the completion of the function, the program process will eject and destroy, return control to the previous execution environment

When the code executes in an environment, the scope chain of the variable object is created, and the fundamental meaning is to ensure that the variables in the execution environment are accessed in an orderly manner at different levels of execution, to see the following code:javascript

var color = "blue";

function changeColor(){

var anotherColor = "red";

function swapColors(){

var tempColor = anotherColor;

anotherColor = color;

color = tempColor; //在 swapColors函数里面可以访问tempColor,anotherColor和color } //在这里可以访问anotherColor和color,但不能访问tempColor swapColors(); }
You can look at the following diagram more visually:

By using the illustration and the diagram, it can be seen that the internal environment can access all the external environments on top of it through the scope chain, but the external environment cannot access any variables and functions in the internal environment. These environments are linear and sequential.

Each environment can search up the scope chain to query for variables and function names, and any environment cannot enter another execution environment by searching the scope chain down

The above paragraph is very important, let's take a look at the example:

javascript var color = "blue"; f

unction getColor(){ return color; }

alert(getColor()); //"blue"
It is obvious that the color variable inside the GetColor function is obtained from the inside search to the color of the parent's external environment:
In the search process, if a local variable with the same name appears, the search stops:

javascript var color = "blue";

function getColor(){ var color = "red"; return color; }

alert(getColor()); //"red"

JavaScript variable Scope!

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.