JavaScript: Variables

Source: Internet
Author: User

Based on the nature of the loosely typed JavaScript variable, a variable is simply a name used to hold a particular value at a specific time. Because there is no rule that defines what data type values A variable must hold, the value of the variable and the data type can be changed over the lifetime of the script.

Values for base types and reference types

JavaScript variables may contain values for two different data types: primitive type values and reference type values. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may have multiple values.

When assigning a value to a variable, the parser must determine whether the value is a base type value or a reference type value. Basic data type: Undefined, null, Boolean, number, and string. These 5 basic data types are accessed by value , so you can manipulate the actual values that are saved in the variable.

  the value of a reference type is an object that is saved in memory. JavaScript does not allow direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly. When you manipulate an object, you actually manipulate the object's reference rather than the actual object. Therefore, the value of the reference type is accessed by reference.

1. 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 different types of values are different. For values of reference types, you can add properties and methods to them, or you can delete properties and methods. For example:

var New  = "Zhang San"//  Zhang San

The above creates an object and saves it in the variable person, then adds the Name property to the object and assigns the string value "Zhang San" to this property. The new property is then accessed through the alert () function.

However, you cannot add attributes to the values of the base type.

var name = "Zhang San"= "a"; alert (name.age);     // undefined
2. Copying variables

When you assign a value from one variable to another variable, both the base type value and the reference type value, the base type and the reference type are different. Copying a base type value from one variable to another creates a new value on the variable object and assigns the value to the location assigned to the new variable.

var num1 = 2; var num2 = NUM1;

The value saved in NUM1 is 2. When NUM2 is initialized with the value of NUM1, the value in Num2 is also 2. However, the values in NUM1 and num2 are independent . This value is just a copy of 2 in NUM1. These two variables can participate in any operation without affecting each other .

When a value of a reference type is assigned to another variable from one variable, the value of the stored variable object is also assigned to the new variable. The difference is that the copy is actually a pointer, and this pointer points to an object stored in the heap. After the assignment operation, two variables will actually refer to the same object . Therefore, changing one of the variables will affect the other variable.

var New Object (); var obj2 == "Zhang San"; alert (obj2.name);    // "Zhang San"
3. Passing parameters

The parameters of all functions in JavaScript are passed by value. Copy the values from the outside of the function to the parameters inside the function, just as you would assign a value from one variable to another. The delivery of a base data type value is like a copy of a primitive type variable, whereas a reference type value is passed as a copy of a reference type variable.

When you pass a value of a primitive type to a parameter, the passed value is assigned to a local variable. When a variable of a reference type is passed to a parameter, the value is assigned to a local variable in the memory address , so the change of the local variable is reflected outside the function.

function Addten (num) {    + =ten    ; return num;} var count =; var result = Addten (count); alert (count);    // No change alert (result);  //  -

The function Addten () has a parameter num, which is actually a local variable of the function. When this function is called, the variable count is passed as a parameter to the function, and the value of the variable is 20. The value 20 is then assigned to the parameter Num. Inside the function, the value of the parameter num adds 10, but this change does not affect the count variable outside the function. The parameter num and count are independent of each other and only have the same value. If NUM is passed by reference, the value of the variable count is also changed to 30.

function setName (obj) {    = "rustling the Cold";} var New  //  "rustling the Cold"

The above creates an object and saves it in the variable person. The variable is then passed to the SetName () function and is copied to obj. Inside this function, obj and person refer to the same object. Then when you add the property name to obj inside the function, the person object outside the function reacts because the object that the person points to has only one and global object in the heap memory.

To prove that the object was passed by value, look at an example:

function setName (obj) {    = "rustling the cold";     New Object ();     = "John Doe";} var New  //  "rustling the Cold"

After the person is passed to the SetName (), its property name is set to "rustling the cold." Then, assign a new object to the variable obj, and set its Name property to "John Doe". If the person is passed by reference, the person is automatically modified to be the object with the name attribute value of "John Doe". However, when accessing Person.name, the values displayed are still "rustling the cold." This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when you override obj within a function, the variable refers to a local object. When the function finishes executing the object, it is destroyed.

3. Type detection

typeof is used when detecting the base data type, but when detecting reference type values, we need to know what type of object a value is. For this, JavaScript provides the instanceof operator. The syntax rules are:

instanceof Constructor

If the variable is an instance of a given reference type, then the instanceof operator returns TRUE. For example:

var New Object (); var i = ten; var c = [' Red ', ' Blue 'instanceof Object);     // true instanceof Object);     // false instanceof Array);      // true

JavaScript: Variables

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.