Value of the basic types and reference types [review base JavaScript (a)]

Preface:

JavaScript variables and other variables is very different languages. JavaScript variables loose nature of the type, it determines that it is only used to save a name of a particular value at a particular time only. Since there is no definition of what type of data a variable rule value must be preserved, and the value of the data type of a variable can change during the lifetime of the script. Despite some ways, this may be an interesting and powerful, but also easy to go wrong properties, but the actual complexity of JavaScript variables is far more than that

Basic types of value type and reference

ECMAScript variable may comprise two values of different data types: basic type value and the reference value type . The basic value refers to a simple type of data segments, and the type value of those reference objects may be composed of a plurality of values.

When assigning a value to a variable, the parser must determine this value is a basic type value or reference type value . There are 7 basic data types: Undefined, Null, Boolean, Number , String, symbol and bigint. These seven types of basic data accessed by value, since the operation can be stored in the actual value of the variable.

Values ​​of reference types are object stored in memory. Unlike other languages, JavaScript does not allow direct access to memory location that is not directly manipulate memory objects. In operation the object is actually referenced in the operation of the object and not the actual object. For this reason, the value of reference types are accessed by reference

(This statement is not tight when copying holds a variable object, the operation is a reference to the object, but when you add a property to the object, the operation is the actual object .-- Turing community "front end Zhuangzhuang Road "Note).

Copy the value of the variable

In copied from a variable to another variable primitive values and reference type value , there are also different . If the value of the basic types of copy variable from one variable to another, will create a new value in the variable object, then the value is copied to the new variable is assigned. Look at an example:

var num1 = 5; 
var num2 = num1;

When copying a reference value from another variable to the type of a variable, the value will also be stored in the variable copy object into the space allocated for the new variable. The difference is that this is actually a copy of the value of the pointer , and the pointer to an object stored on the heap. After the copy operation, two variables actually refer to the same object. Thus, changing one of the variables, it will affect the other, as shown in the following example:

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

Passing parameters

ECMAScript functions All parameters are passed by value That is, the external copy function value to a parameter in the function, and the values on a copy from the same variable to another. Primitive values as transmitted copy of the same basic type of variable, the value of the reference delivery type, as the type of replication as a reference variable. There are a lot of developers at this point may be confused, because access variables by value and by reference two ways, and the parameters can only be passed by value.

When passing the parameter value of the basic type, the value is passed will be copied to a local variable (i.e. named parameters, or use the concept of ECMAScript, it is an object element arguments). When the value of type reference to the parameter passing, interprets the value of the address in memory copied to a local variable, so the change will be reflected in the local variable external function.

//例子1 基本数据类型作为参数

function addTen(num) { 
 num += 10; 
 return num; 
}
var count = 20; 
var result = addTen(count); 
alert(count); //20,没有变化
alert(result); //30

//例子2 引用类型作为参数
function setName(obj) { 
 obj.name = "Nicholas"; 
} 
var person = new Object(); 
setName(person); 
alert(person.name); //"Nicholas"

Some people mistakenly believe that: modify objects in the local scope will be reflected in the global scope, it shows the parameters are passed by reference. In order to prove that the object is passed by value, we'll take a look at this example, the following modified:

//例子1 参数按值传递
function setName(obj) { 
 obj.name = "Nicholas"; 
 obj = new Object(); 
 obj.name = "Greg"; 
} 
var person = new Object(); 
setName(person); 
alert(person.name); //"Nicholas"

This shows that even if modify the values ​​of the parameters within the function, but the original reference remain unchanged. In fact, when the internal function overrides obj, this variable is a reference to a local object. And this local object will be destroyed immediately after the function is finished.

Detection Type

要检测一个变量是不是基本数据类型?第 3 章介绍的 typeof 操作符是最佳的工具。说得更具体一点,typeof 操作符是确定一个变量是字符串、数值、布尔值,还是 undefined 的最佳工具。如果变量的值是一个对象或 null,则 typeof 操作符会像下面例子中所示的那样返回"object":

var s = "Nicholas"; 
var b = true; 
var i = 22; 
var u; 
var n = null; 
var o = new Object(); 
alert(typeof s); //string 
alert(typeof i); //number 
alert(typeof b); //boolean 
alert(typeof u); //undefined 
alert(typeof n); //object 
alert(typeof o); //object

通常,我们并不是想知道某个值是对象,而是想知道它是什么类型的对象(包含普通对象Object,数组对象Array,正则对象RegExp,日期对象Date,数学函数Math,函数对象Function)。为此,ECMAScript提供了instanceof 操作符,其语法如下所示:

result = variable instanceof constructor

alert(person instanceof Object); // 变量 person 是 Object 吗?
alert(colors instanceof Array); // 变量 colors 是 Array 吗?
alert(pattern instanceof RegExp); // 变量 pattern 是 RegExp 吗?

根据规定,所有引用类型的值都是 Object 的实例。因此,在检测一个引用类型值和 Object 构造函数时,instanceof 操作符始终会返回 true。当然,如果使用 instanceof 操作符检测基本类型的值,则该操作符始终会返回 false,因为基本类型不是对象。

参考书籍 JavaScript高级程序设计

Guess you like

Origin www.cnblogs.com/riwang/p/12389433.html