preface

Because JS is a weakly typed language, according to ecMA-262, js variables are very different from other variables, including that there is no definition of which variable must hold which numeric type. For example, in the Java language specification, String values must be explicitly declared as String. Js uses var declarations uniformly, and ES6 provides let and const variable declarations

JavaScript defines two different types of variables: primitive numeric types, which refer to simple data segments, and reference data types, which refer to objects that may consist of multiple values

Chestnut 😀 in js specification, there are five basic numeric types, respectively is Undefined, Null, Boolean, Number and a String, the five kinds of basic data types which are accessed by value, because the can be stored in the variable operation of the actual value. Reference type is a generally kept in memory object, in accordance with the contract, js does not allow direct access to the location in memory, that is not directly manipulate the object’s memory space, so we in the operation of the object is actually in the operating object reference, not the actual object (the value of a reference type is accessed by reference)

Dynamic properties

Define a basic types and reference types are similar, is to create a variable and assignment for him, but when this value is stored for different types of values can do is not the same as for a reference type, we can dynamically to add properties or methods for him, this passage to IDE the following input, You can also do what I did F12 using Google’s console

var person = new Object();
person.name='Tan Jing Jie';
console.log(person.name)

Copy the code

Yes, we created an object and stored it in Person. The property named name did not exist at the beginning of creation. We used the object name. The property name method dynamically creates the property name and assigns it a value. If the object is not destroyed or deleted, the property will always exist. But for primitive types, we cannot add properties to primitive types. This property will change to Undefined, in the case of a value

Var a = 12.3; console.log(a.toFixed(3));# output "12.300"

a.foo = 'bar';
console.log(a.foo);# output undefined
Copy the code

Copy copies of variables from variable to variable

In addition to the above dynamic saving, copying between variables is also different. For primitive types, a new value is created on the variable object and then copied to the type assigned to the new variable

Note that these two values do not affect each other; they are independent

For reference types, the value stored in the variable object is also copied into the memory space of the new variable, but the copy of the value is actually a pointer to the object to which we are assigned, so that the assignment of the reference type is actually the same object as the reference, anyway. Okay

Ignoring the minor incident in the middle, you can see that I’m not manipulating the Jack variable, but it changes with Tom

Parameter passing in a function

All functions in ECMAScript are passed by value. When passing a primitive type variable, the passed value is copied to an element in a arguments object. When passing a reference type value, its memory address is copied, enter

function addTen(num) {    
   num += 10;    
   return num;
} 
var count = 20;
var res = addTen(count);
alert(count);   # 20 doesn't change anything
alert(res)      # 30 
Copy the code

When we replace a primitive type with an object reference type


 function setName(obj) {     obj.name = "Nicholas"; } 
 
var person = new Object(); setName(person); alert(person.name);    # "Nicholas" 
Copy the code

In the above code, obj and Person refer to the same object. The code to which Person points exists only one in the heap (the global object). Obj accesses the same object by reference even though the variable obj is passed by value

Ps: When the function overrides obj internally, this variable references a local object. The local object is destroyed immediately after the function completes execution

The last

If a variable is a primitive type, you can use typeOf, but for reference types typeOf returns an Object. Because all reference types should be instances of Object, you can use instanceof instead. Instanceof is called an instance, like person instanceof Object, is the variable person an Object? Return true if yes