JavaScript variables, scope, and memory issues

Source: Internet
Author: User

1. Basic types and reference types

JavaScript variables may contain values for two different data types: primitive type values and reference type values. Primitive type values refer to simple data segments that are stored in the stack's memory, meaning that this value is completely stored in a single location in memory. The reference type value refers to the object that is stored in the heap memory, meaning that the variable is actually just a pointer to another location in memory where the object is saved.

The values of the JAVASCRIPT5 basic data types occupy a fixed size of space in memory, so they can be stored in the stack memory. Also, this can increase the speed of query variables. For variables that hold base type values, we say they are accessed by value. If you assign a value to a variable that is a reference type, you must allocate space for the value in heap memory. Because the size of this value is not fixed, they cannot be saved to the stack memory. However, the size of the memory address is fixed, so the memory address can be stored in the stack memory. Thus, when querying a variable of a reference type, it is possible to first read the memory address from the stack and then "trace" the value stored in the heap. For this way of querying variable values, we call it access by reference.

Dynamic Properties

You define a primitive type value and a reference type value in a similar way, create a variable and assign a value to the variable. However, when this value is saved to a variable, the actions that can be performed on the different types of values are quite distinct. For values of reference types, we can add properties and methods to them, and you can change and delete their properties and methods:

var person = new Object ();p erson.name = "Nichoias"; alert (person.name);

As in the example above, this property will persist if the object is not destroyed or if the property is not deleted. However, we cannot add properties to the values of the base type, although doing so does not cause any errors:

var name = "Nicholas"; name.age = 27;alert (name.age); Undefined

Copy variable values

In addition to the way it is saved, there is a difference between copying the base type value and the reference type value from one variable to another. If you copy the value of the base type from one variable to another, a new value is created in the stack, and the value is copied to the location assigned to the new variable:

var num1 = 5;var num2 = NUM1;

In this case, the value saved in NUM1 is 5. When NUM2 is initialized with the value of NUM1, the value 5 is also saved in num2. However, 5 in num2 and 5 in NUM1 are completely independent, and this value is just a copy of 5 in NUM1. Thereafter, these two variables can participate in any operation without affecting each other.

When a value of a reference type is copied from one variable to another, the value stored in the stack is also copied into the space allocated for the new variable. The difference is that a copy of this value is actually a pointer to an object stored in the heap. When the copy operation is finished, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the other variable:

var obj1 = new Object (), var obj2 = Obj1;obj1.name = "Nicholas"; alert (obj2.name); "Nichoias"

Pass parameters:

The parameters of all functions in JavaScript 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. The delivery of a primitive type value is like a copy of a primitive type variable, whereas a reference to a value of a type is the same as a copy of a reference type variable.

When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, or, in the sense of JavaScript, an element in the arguments object). When a value of a reference type is passed to a parameter, the address of the value in memory is copied to a local variable, so the change of the local variable is reflected outside the function:

function Addten (num) {num + = 10; return num;}                     var count = 20;var result = Addten (count); alert (count);                     20, no change alert (result); 30
function SetName (obj) {obj.name = "Nicholas";}                var person = new Object (), setName (person), alert (person.name); "Nicholas"


JavaScript variables, scope, and memory issues

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.