js primitive data types, reference data types

js data type division manner the original data type and reference data types

Stack: primitive data types (Undefined, Null, Boolean, Number, String)

Heap: reference data types (objects, arrays, functions)

Two types of differences:

Different storage locations

Simple primitive data types stored directly on the stack (Stack) in the data segment, small footprint, a fixed size, are frequently used data, it is stored on the stack;
reference data types stored directly in the stack, occupies a large space size is not fixed, if stored on the stack, will affect the running performance, the type of reference data is stored in the stack pointer, the pointer to the start address of the stack entity, the interpreter when looking reference value, it will first retrieve in the stack address, after obtaining the address of the entity obtained from the heap.

Different by value

Passed by value (call by value) is the most commonly used evaluation strategy: the function parameter is transmitted is called a copy argument. Modify the value of the parameter does not affect the argument.

When passed by reference (call by reference), the shape function parameters received implicit reference argument, instead of the copy. This means that the reference value is modified if the function form, the argument will be modified. Both point to the same value simultaneously.

By reference to track it more difficult to make the transfer function calls, and sometimes cause some subtle BUG.

Passed by value due to the need to clone a copy of each of a number of complex types, lower performance. Two kinds of value-passing methods have their own problems.

For example:

  var person,name;
    person = 'kn';
    name=person;
    person='黑白';
    console.log(person,name,typeof person)//黑白 kn string

change no change person name, description string is passed by value. Creating a new memory space assignment

1, the basic data types: passed by value

Immutable (immutable) nature of the
basic types are immutable (immutable), only the object is variable (mutable). Sometimes we try to "change" the contents of the string, but in JS, seemingly for any string value "modify" operation, are actually creating a new string value. Any method can not change the value of a primitive type,

    var str = "abc";
     str[0]; // "a"
     str[0] = "d";
     console.log(str); //abc
     var name = 'jozo';
     var upName=name.toUpperCase(); 
     console.log(upName,name); // 输出 'JOZO' 'jozo'

The method of operation can not substantially change the value of a type

var person = 'kn';
     person.age = 24;
     person.method = function(){};
     console.log(person.age); // undefined
     console.log(person.method); // undefined

2. The reference type: passed by reference

Reference value variable type

 var obj = {x : 0};
     obj.x = 100;
     var o = obj;
     o.x = 1;
     console.log(obj.x)// 1, 被修改
    o = {x:100};  //等同于重新赋值,重新开辟内存,不是修改
    console.log(JSON.stringify(obj),JSON.stringify(o))//{"x":1} {"x":100}
    obj.x; // 1, 不会因o = {"x":100}改变

Values ​​of reference types are simultaneously stored in the object stack memory and heap memory

javascript and various other languages, its position does not allow direct access to memory, the memory space that is not directly manipulate objects, then we operate Shane? In fact, the operation is a reference to the object,
so the reference value is the type of access by reference.
Rather, reference type of memory required to store the stack area and heap area (heap area is the memory of heap memory) have completed pointer stack area of memory stored variable identifier and point to heap memory for the object,
it can be said this object is in heap memory address.
If you have the following objects:

var person1={name:"xiaoming"}
var person2={name:"xiaohong"}
var person3={name:"xiaoxiao"}

Difference between the two arguments and data types of the formal parameters

We must first understand what the argument is what parameter.
Argument: can be constants, variables, expressions, functions, etc., regardless of what type of argument that the amount of, during function calls, they must have a value determined so as to transfer the values to the parameters. It should therefore be assigned in advance with, other ways to make an input argument obtained determination value.
Parameter: called "formal parameters" are parameters used in the definition of the function names and function of the body when the object is used to receive the parameters passed when calling the function.
Parameter might be mediated link between the calling function and the called function, the function is typically processed data, processing or function of factors that affect the function as the function parameter.

function addNum(param)  //相对于addNum方法来说 param是形参
{ 
    param+=10; 
     return param; 
} 
var num=10;  
var result=addNum(num); //相对于addNum方法来说 num是实参
console.log(num);  //10  
console.log(result);//20

In the above example, when the method num as an argument passed as parameter param addNum is accepted and used in the in vivo method, and the global change num useless, but when the argument is a reference type

function fun(param)  //相对于fun方法来说 param是形参
 { 
    param[0]=99; 
     return param; 
} 
 var num=[10];  
 var result=fun(num); //相对于fun方法来说 num是实参
 console.log(num[0]);  //99 
 console.log(result);//[99]

In the process of changing the parameter in vivo, will also change the argument

Reference:
JS original data type and the reference data type => callback data transmission principle

Guess you like

Origin www.cnblogs.com/sunny-shine/p/11350701.html