JavaScript Value Delivery and scope

Source: Internet
Author: User

ECMAScript variables are loosely typed, and when assigning a value to a variable, the parser must determine whether the value is a base type or a reference type. The base type is accessed by value, because the actual value that is saved in the variable can be manipulated.
The value of a reference type is an object that is stored in the heap memory, and JavaScript does not allow direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly. When an object is manipulated, it is actually a reference to an object instead of
Manipulate the actual object. Therefore, the JavaScript reference type is accessed by reference.

For reference types we can add properties and methods to him, but we cannot add attributes to the values of the base type, although doing so does not result in any errors. As shown below:
var num = 1;
Num.age = 12;//Here implicitly calls number () and assigns it a value, so there is no error. The object is dismissed after the execution has finished.
alert (num.age); Undefined

@ Copy Variable
In addition to saving differently, there are differences when one variable copies the base type and reference type to another variable. If you copy a base type value from one variable to another, a new value is created on the variable object and the value
Copy to the location assigned to the new variable. As shown below:

var num = 12;
var age = num;
Age = 24;
alert (num);//12
alert (age);//24

In the above example, the NUM variable holds 12 and then copies num to the new variable, age, which is a copy of Num. Then the value of age is modified, the last 2 values are printed, and the change of age does not change the value of Num.

When you copy the value of a reference type from one variable to another, the value stored in the variable object is also copied into the memory space allocated for the new variable. The difference is that the copy of the value is actually a pointer, and the pointer
Points to an object that is stored in heap memory, and after the copy operation ends, two variables actually refer to the same object. So changing one of these variables will affect the other. As shown below:

var po = {
Age:24,
Name: "Tony"
}

var ob = PO;
Ob.name = "Miracle";
alert (po.name);//miracle
alert (ob.name);//miracle

As shown in the preceding code, modifying the Name property of the copied variable OB affects the Name property of the original PO object because the two variables refer to the same object, and their value is a pointer to that object. Sum up:
* Reference type replication is actually a copy of the pointer.

@ parameter Passing
The parameters of all functions in ECMAScript are passed by value. In other words, copying the values from the outside of the function to the parameters inside the function is the same as copying the value from one variable to another. Basic type values are passed as basic types
The same as the copy of a variable, and the passing of a reference type value is like a variable copy of a reference type. When you pass a primitive type value to a parameter, the passed value is copied to a local variable. When you pass a reference type to a parameter, the value is in memory
is copied to a local variable, so changes to this local variable are reflected outside of the function. As shown below:

function Basicfun (num) {
num + = 12;
return num;
}

var sum = 12;
var age = Basicfun (sum);
alert (sum);//12
alert (age);//24

As shown in the code above, define a sum whose value is 12, then call Basicfun () to copy sum to parameter num, and then in the Basicfun () function, the parameter num does +12 operations, but this operation does not affect
The external variable sum of the function. Parameter num is not acquainted with the variable sum, they just have the same value. Using basic types is simple, and if you use reference types it can be a little more complicated. Take a look at the following code.

function Quotefn (obj) {
Obj.name = "Tony";
Obj.age = 24;
obj = new Object ();
Obj.sex = "Men";
}

var person = {
Name: "Miracle",
Age:12
}

Quotefn (person);
alert (person.name);//tony
alert (person.age);//24
alert (person.sex);//undefined;

The above code creates a person object with the Name property value of "miracle" and an age property value of 12. The variable is then passed to Quotefn () and copied to the parameter obj,
Within this function the parameters obj and the variable person refer to the same object. In other words, even if the variable is passed by value, obj accesses the same object by reference, as obj modifies the name and
After the value of the Age property, the external person will also have the same reaction. Because a reference type replicates a pointer to an object. The 3rd line of code in the Quotefn () function is re-created for the parameter obj
An object and adds an attribute sex to it. If the person is passed by reference, the person will brake to point to the new object. But next access to the new property, sex, shows undefined;
This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when you rewrite obj in a function, this variable refers to a local object. And this local object is executed in the function
is destroyed immediately after completion.

@ Detection Type
To detect whether a variable is a base type using the TypeOf operator, and the detection object uses the instanceof operator, the typeof operator returns a string representation of the operation type.
The instanceof operator returns a Boolean value, as shown below;

Alert (typeof true); Boolean
Alert ([] instanceof Array); True

@ Execution Environment and scope
There is no block-level scope in JavaScript, he has global scope and function scope (local scope), and the global execution environment is the outermost execution environment. Depending on the hosting environment where the ECMAScript implementation resides, the objects that represent the execution environment are different.
In a Web browser, the global execution environment is considered a Window object, so all global variables and functions are properties and methods of the Window object. After all the code in an execution environment has been executed, the environment is destroyed, all variables stored in it, and
The function definition is also destroyed (the global execution environment knows that the app exits-columns such as closing a Web page or browser).
About a scope chain is an internal scope that can access an external scope.

JavaScript Value Delivery and scope

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.