JavaScript variables those things

Source: Internet
Author: User

Introduction

JavaScript's variable nature is loosely typed, meaning its variable is a name used to hold a particular value, and the value and data type of the variable can change in the life cycle of the script execution. This is a very interesting and powerful feature, but it is also a very easy place to make mistakes.

Two major types

Basic type : Refers to a simple data segment, there are five basic types: undefined,null,boolean,number,string. These basic types are accessed by value, and manipulating variables is the actual value saved in the variable.

reference type : Refers to which objects (object) may be composed of multiple values. The value of a reference type is an object that is saved in memory. It is important to note that JavaScript does not allow direct access to in-memory locations, so manipulating objects is actually a reference to an operand (this is not strictly true, because when you copy a variable that holds an object, the object's reference is manipulated, but when you add a property to an object, it is the object itself).

Replication of a variable's copy base type variable

When copying a variable of the base type, it is actually creating a new value on the variable object and assigning the new value created to the location where the new variable is copied. So the original variable and the assigned variable are independent in memory, and the two variables can participate in any operation without affecting each other.

var a = ' test '; var b == ' Change '; Console.log (b); // Output: Test
Replication of reference type variables

When copying the value of a reference variable, a copy of the value stored in the variable object is also placed in the newly allocated space. But it's worth understanding that a copy of this value is actually a pointer to an object stored in the heap memory. As stated above: For variables of reference types, JavaScript does not allow direct manipulation of objects, but rather the manipulation of references to objects, so a pointer to the actual storage space is stored in the reference variable. After copying the end of the two variables actually refer to an object, so change one of the variables and the other will be affected.

var New Object (); var b == ' Test '; Console.log (b.title); // Output: Test

The relationship between the variables stored in the variable object and the objects held in the heap (don't mind the ugly image)

Parameter passing

The parameters of all functions in ECMAScript are passed by value. What do you mean? is to copy the values outside the function body to the parameters inside the function (as if copying the values from one variable to another). That would be a good idea. and understand the above variable replication, it is very good to understand the parameters in JS transfer characteristics.

When passing a primitive type value to a parameter, the passed value is assigned to a local variable, the named parameter of the function body (in ECMAScript, an element in the arguments object). When a value of a reference type is passed to a parameter, the value is assigned to the parameter in the memory address.

function setName (n) {    = ' Test ';     return  var a = ' haha '; var b = setName (a); Console.log (a); Console.log (b); // Output: // haha // Test

If you use a reference type as a parameter:

function setName (obj) {    = ' Shanlei ';} var New Object (); SetName (a); Console.log (A.name) ; // Output: Shanlei

In this function, obj and a refer to the same object, and when the name attribute is added to the SetName, the outside of the function will react. Here, one might mistakenly assume that an object modified in a local scope is reflected in the global scope, which means that the parameter is passed by reference. The idea is wrong. You can look at the following example:

function setName (obj) {    = ' Shanlei ';     New Object ();     = ' Grep ';} var New Object (); SetName (a); Console.log (A.name) ; // Output: Shanlei

In this paragraph, we redefine obj as an object and define a name property of a different value for the new object. If the argument is passed by reference, a should automatically be modified to point to a new object whose Name property is set to ' Grep '. Because if arguments are passed by reference, then obj and a should be uniform references, which point to the space in the heap where the objects are actually stored. When a new object is generated to set the new Name property and the new object is given to obj, the reference is actually changed to the point in the heap, so the external a will automatically change. The output is still ' shanlei ', which means that the value of the parameter is modified inside the function, but the original reference remains unchanged. Because obj and a are two independent variable objects in memory, the two variable objects point to the same space in the heap, and when a new object is generated and passed to obj, the point of obj changes, but the point of a is still the original space in the heap, so the output is still ' shanlei '. When you override obj inside a function, the variable reference is a local object, and the local object is destroyed immediately after the function is executed.

Above ~ ~

JavaScript variables those things

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.