Explanation of JavaScript Variables

Source: Internet
Author: User
This article focuses on the differences between the values of two different data types contained in JavaScript variables and the basic and reference type values. In addition, the relationship between ECMAScript and JavaScript is omitted. This article focuses on the differences between the two different data types in JavaScript variables-basic type values and reference type values. In addition, the relationship between ECMAScript and JavaScript is slightly included.

It is called a JavaScript variable, but more specifically the ECMAScript variable.

In 1990s, Netscape and Microsoft released two different versions of JavaScript, which is not conducive to the development and use of JavaScript and promoted the European Computer Manufacturers Association (ECMA, European Computer Manufacturers Association) beginning with JavaScript standardization, the well-known ECMA-262 was completed-defining the standard for a new scripting language called ECMAScript.

A complete JavaScript implementation includes ECMAScript, the Document Object Model (DOM, Document Object Model), and the Browser Object Model (BOM, Browser Object Model ). As the core of JavaScript and the basis of implementation, ECMAScript is the language description of the syntax, type, statement, keyword, reserved word, operator and object defined by ECMA-262 standards.

The ECMAScript variable specified by the ECMA-262 standard is loosely typed and can be used to save any type of data, so the operations for different types of initialization variables can be executed in one statement, and the following code is legal.

 var message = "hello",  //string      age = 20,           //number3      found = false;   //boolean 

Variables defined with the var operator will be local variables defining the scope of the variable. After exiting the scope, the variables will be destroyed immediately. For example, if a variable is defined in a function, the variable is created when the function is called. However, after the function exits, the variable cannot be accessed again.

There are six Data Types in ECMAScript (or only six, ECMAScript does not support any mechanism for creating custom types ).

The basic data types include five types: underfined, null, boolean, number, and string. These five basic data types are accessed by value. The value belongs to the basic type value mentioned at the beginning of the article, is a simple data segment that can be used to operate the actual values stored in the variable.

The 6th types are complex data types-objects, which are essentially a group of unordered name-value pairs. They are reference type values and are stored in the memory. JavaScript does not allow direct operations on the object's memory space. When operating on the object, it is actually referencing the object rather than the actual object.

Although you do not need to specify the data type when defining a variable, the basic type and reference type values can perform very different operations.

Add attribute

You can add, change, and delete a value of the reference type for its attributes and methods, as shown in the following code:

Var obj = new object (); // create an object and save it in obj. name = "Marry"; // Add the attribute named name and assign the string value "Marry" alert (obj. name); // the pop-up "Marry"

If the obj object is not destroyed or the name attribute is not deleted, this attribute will always exist.

Let's look at the basic type value:

Var name = "Marry"; // create a string name. age = 20; // Add the attribute named age and assign the number value 20 alert (name. age); // "underfined" is displayed"

The name string is added with an age attribute and assigned a value of 20. However, this attribute will disappear during the next access.

This indicates that attributes can only be dynamically added to reference type values.

  Copy variable value

Copy a value of the basic type from variable a to variable B. a new value is created on the variable B object, and the value is copied to the position assigned to variable a, which is saved independently. These two variables do not affect each other in any operation.

If you copy a value of the reference type from variable c to variable d, the value stored in the variable d object will also be copied to the space allocated for variable c, but the copy of this value is actuallyPointer, And the variable d points to the same object in the heap memory. Two variables actually reference the same object. Changing one of the variables will affect the other.

For specific differences, see the following example:

// Basic type value var num1 = 5; var num2 = num1; num2 = num2 + 5; alert (num1); // 5 alert (num2 ); // 10 // reference type value var obj1 = new object (); var obj2 = obj1; obj1.name = "Marry"; alert (obj2.name); // "Marry"

  Function Parameters

All function parameters in ECMAScript are passed by value, that is, the external values of the function are copied to the internal parameters of the function. Since the basic type value differs from the reference type value copying variable, the function parameter passing effect is also different.

When you pass a basic type value to a parameter, the passed parameter is assigned to a local variable. Changes in the internal parameters of the function do not affect the variables outside the function. When you pass a reference type value to the parameter, the address in the memory is copied to a local variable, so the changes to this local variable will be reflected outside the function. For example:

// Pass the basic type value function addnum (num) {num + = 10; return num;} var num1 = 5; var num2 = addnum (num1); alert (num1 ); // 5, unchanged alert (num2); // 15 // pass the reference type value function setage (obj) {obj. age = 20;} var obj1 = new object (); setage (obj1) alert (obj1.age); // 20

Objects modified in the local scope are reflected in the global scope. Many people think that this is passed by reference. However, function objects are indeed passed by value. See the following example:

function setage(obj) {    obj.age = 20;    obj = new object();    obj.age = 30;}var obj1 = new object();setage(obj1)alert(obj1.age);           //20

In this example, an object is redefined for obj in the function and assigned a value for its age attribute, but this change is not reflected outside the function. It indicates that obj1 is not passed by reference. Objects redefined in the actual function are partial objects, which will be destroyed immediately after exiting the function.

  Detection type

The basic type value can be detected by typeof, but only objects can be returned when typeof detects the reference type. To know what type of object a value is, ECMAScript provides the instanceof operator. The syntax is as follows:

result = variable instanceof constructor

If the variable is of the reference type, the instanceof operator returns true.

The above is a detailed explanation of JavaScript variables. For more information, see other related articles in the first PHP community!

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.