值传递和引用传递
值类型变量:
存在内存的堆中,比如:a=1
引用类型变量 :
1.指针存在于栈中,2.引用类型的具体内容存在于堆中 ex:let a={b:1} a的指针指向 堆中的地址0xffac0ec
正如我在 第二章 说的,
- number
- string
- boolean
这三种常规类型的是值传递, 而array,对象这种就属于引用传递了。
代码来说话,我们先看看
值传递:
var int1 = 12;
var int2 = int1;
int1 = 10000;
console.log(int2);
var b1 = true;
var b2 = b1;
b2 = false;
console.log(b1);
var str1="hello world";
var str2=str1;
str2="高司机,出bug了";
console.log(str1);
输出结果为
12
true
hello world
引用传递:
let obj1 = {};
let obj2 = null;
obj2 &#