Variables in JavaScript

Source: Internet
Author: User

JavaScript is a weakly typed programming language, and its variables are loosely typed. However, there are always a lot of problems with such loosely constrained variable rules, and in fact, the variables in JavaScript actually contain the values of two data types: the base type value and the reference type value, and the following is a distinction between the two forms of variable values.

First, to be clear, a variable is simply a container that holds different values, so all we do is operate on the values stored in the variable. These two types of values are first differentiated in nature. First, the way of preservation is different. The value of the base type is simply a string, it has no properties and methods (such as the length property of the string, and the parseint () method, etc.), we mean that we can not add properties to them, and the value of the reference type is actually an object (object), Not only do they have their own method attributes, we can also add properties to them, for example:

var New  Object (); // defining an Object   = ' 123456 '; // added a property to this object
alert (ostudent.id); //123456

Second, the operation is accessed in different ways. The basic type of the value as an exact data segment, so that in our operation, we access it by value access, that is, any operation we do, in fact, the actual value of the variable to operate, for example, after it is modified, its original value will be destroyed, no longer exist. The value of a reference type differs from the value because it is an object stored in memory, and JavaScript does not allow us to directly access the object in the memory address, while manipulating the object, actually manipulating the reference to an object, not the object itself. We can also interpret it as a pointer-like thing.

Thirdly, the way of reproduction is different. From the 2nd different, we can actually know that if you want to copy a variable, the value of the basic type is not interfering with each other, so that the copy of the variable is a new variable, and the original is not related, such as var String1 = "Hello World"; var String2 = String1; This time String1 and String2 are independent of each other two variables, you can change the value of String2, STRING1 will not be affected. The value of a reference type is not the same, and when copied from a variable of a reference type to another variable, the value stored in the variable is copied in the memory space of the new variable, but this is not exactly a completely new thing to copy, but just a pointer, This pointer also points to the object in the store, which is actually equivalent to copying the two variables that refer to the same object when you change one of the values, and the value of the other variable changes. Let's give an example, the value of the reference type is equivalent to a large treasure in the forest, we have two copies of the same treasure map to two people, when one of them took the lead in obtaining the treasure, the other person can only be returned without work, in the middle, two copies of the treasure map is identical, equivalent to the copied two variables, And these two copies of the treasure map point to a common value, that is a this treasure. Let's go back to the previous example and try to practice:

var New  Object (); // defining an Object var oStudent1 = ostudent;      // copy one of the same        objects  ostudent.id = ' 123456 '; // added an attribute to this object ostudent1.id = ' 12345678 ';        //  OStudent1 modified the value of the ID attribute alert (ostudent.id); // If I get the ID of ostudent, will there be any change? 

Of course the result is the same as we imagined, Ostudent's ID was also modified.

After reading the difference between the values of the base type and the value of the reference type, we certainly need to know what the basic types of values are in JavaScript and what the reference types are. In JavaScript, there are five basic types of values, namely: Undefined, Null, Boolean, number, and string. The value of a reference type is an object, in JavaScript, the values of these reference types are encapsulated in data and functionality, forming some handy reference types, commonly: Object type, array type, date type, regexp type, function type. Of course, we'll learn more about it later.

Variables in JavaScript

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.