4.1 Values for basic types and reference types "JavaScript Advanced Programming Third Edition"

Source: Internet
Author: User

The ECMAScript variable may contain values for two different data types: the base type value and the reference type value. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of 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. The 3rd chapter discusses 5 basic data types: Undefined, Null, Boolean, number, and string. These 5 basic data types are accessed by value because 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. Unlike other languages, JavaScript does not allow direct access to in-memory locations, which means that the object's memory space cannot be manipulated directly. When you manipulate an object, you are actually manipulating the object's reference rather than the actual object. To do this, the value of the reference type is ① accessed by reference.
In many languages, strings are represented in the form of objects and are therefore considered reference types. ECMAScript has given up on this tradition.

4.1.1 Properties of the dynamic

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. Take a look at the following example:

① This is not a strict statement, when copying a variable that holds an object, the action is the reference to the object. However, when you add a property to an object, you are manipulating the actual object.
var person = new Object ();p erson.name = "Nicholas"; alert (person.name); "Nicholas"

Run for a minute
The above code creates an object and saves it in the variable person. We then added a property named name to the object and assigned the string value "Nicholas" to this property. The new property is then accessed through the alert () function. 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 result in any errors. Like what:

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

Run for a minute
In this example, we define a property named age for the string name and assign a value of 27 to the property. However, when this property is accessed on the next line, the attribute is found to be missing. This means that you can add properties dynamically to reference type values for future use.

4.1.2 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 on the variable object, and the value is copied to the location assigned to the new variable. Take a look at an example:

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. Figure 4-1 Visually shows the process of copying the base type values.


When a value of a reference type is copied from one variable to another, the value stored in the variable object 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, as shown in the following example:

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

First, the variable obj1 holds a new instance of an object. The value is then copied to the Obj2; in other words, both obj1 and Obj2 point to the same object. This way, when the Name property is added for Obj1, this property can be accessed through obj2 because both variables refer to the same object. Figure 4-2 shows the relationship between the variables saved in the variable object and the objects held in the heap.

4.1.3 Passing parameters

The parameters of all functions in ECMAScript 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. There are a number of developers who may be confused at this point, because access variables are both by value and by reference, and parameters can only be passed by value.

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 ECMAScript concept, 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. Take a look at the following example:

function Addten (num) {    num + = ten;    return num;} var count = 20;var result = Addten (count); alert (count); 20, no change alert (result); 30

Run for a minute
Here the function Addten () has a parameter num, and the argument 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 copied to the parameter num for use in Addten (). Inside the function, the value of the parameter num is added to 10, but this change does not affect the count variable outside the function. Parameter num is not acquainted with the variable count, they simply have the same value. If NUM is passed by reference, then the value of the variable count is also changed to 30, which reflects the changes inside the function. Of course, using basic types such as numeric values to illustrate that passing parameters by value is simple, but if you use objects, the problem is not very well understood. One more example:

function SetName (obj) {    obj.name = "Nicholas";} var person = new Object (), setName (person), alert (person.name); "Nicholas"

Run for a minute
The above code creates an object and saves it in the variable person. The variable is then passed to the SetName () function and is then copied to obj. Inside this function, obj and person refer to the same object. In other words, even if the variable is passed by value, obj accesses the same object by reference. Therefore, when you add the Name property to obj inside the function, the person outside the function will also be reflected, because the object pointed to by person has only one and global object in the heap memory. There are many developers who mistakenly assume that objects modified in a local scope are reflected in the global scope, stating that the parameters are passed by reference. To prove that the object is passed by value, let's take a look at the following modified example:

function SetName (obj) {    obj.name = "Nicholas";    obj = new Object ();    Obj.name = "Greg";} var person = new Object (), setName (person), alert (person.name); "Nicholas"

The only difference between this example and the previous example is that two lines of code are added to the SetName () function: One line of code redefined an object for obj, and the other line defines a name property with a different value for the object. After the person is passed to SetName (), its Name property is set to "Nicholas". Then, assign a new object to the variable obj, and set its Name property to "Greg". If person is passed by reference, the person is automatically modified to point to a new object whose Name property has a value of "Greg". However, when you next access Person.name, the value displayed is still "Nicholas". This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when you rewrite obj inside a function, the variable refers to a local object. This local object is destroyed immediately after the function is executed.

you can think of the parameters of the ECMAScript function as local variables.
4.1.4 Detection Type

Do you want to detect if a variable is a basic data type? The typeof operator described in the 3rd Chapter is the best tool. To be more specific, the TypeOf operator is the best tool for determining whether a variable is a string, a numeric value, a Boolean, or a undefined. If the value of the variable is an object or null, the TypeOf operator returns "Object" as shown in the following example:

var s = "Nicholas"; var b = True;var i = 22;var u;var n = null;var o = new Object (); alert (typeof s); Stringalert (typeof i); Numberalert (typeof B); Booleanalert (typeof u); Undefinedalert (typeof N); Objectalert (typeof O); Object

Run for a minute
Although TypeOf is a very powerful helper when detecting a basic data type, this operator is of little use when detecting the value of a reference type. In general, we do not want to know that a value is an object, but rather to know what type of object it is. To do this, ECMAScript provides the instanceof operator, whose syntax is as follows:

result = Variable instanceof constructor

The instanceof operator returns true if the variable is a given reference type (identified by its prototype chain) and an instance of the prototype chain is introduced in the 6th chapter. Take a look at the following example:

Alert (person instanceof Object); is the variable person an object? Alert (colors instanceof Array); is the variable colors an array? Alert (pattern instanceof REGEXP); is the variable pattern a regexp?

As a rule, all reference types have values that are instances of object. Therefore, the instanceof operator always returns True when a reference type value and an object constructor are instrumented. Of course, if you use the instanceof operator to detect a value of a base type, the operator always returns false because the base type is not an object.

when you use the TypeOf operator to detect a function, the operator returns "function". When using typeof to detect regular expressions in Safari 5 and previous versions and in Chrome 7 and earlier, this operator also returns "function" for canonical reasons. ECMA-262 specifies that any object that implements the [call] method internally should return "function" when the typeof operation is applied. Because the regular expression in the above browsers also implements this method, applying typeof to the regular expression returns "function". In IE and Firefox, applying typeof to Regular Expressions returns "Object".
More Chapters Tutorial: http://www.shouce.ren/api/view/a/15218

4.1 Values for basic types and reference types "JavaScript Advanced Programming Third Edition"

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.