Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

赋值、浅拷贝与深拷贝 #11

Open
wanCheng7 opened this issue Feb 11, 2020 · 0 comments
Open

赋值、浅拷贝与深拷贝 #11

wanCheng7 opened this issue Feb 11, 2020 · 0 comments

Comments

@wanCheng7
Copy link
Owner

wanCheng7 commented Feb 11, 2020

数据类型

分为基本数据类型引用类型

基本数据类型包括6种:stringnumberbooleannullundefinedsymbol

除了这几种基本类型,其他的都是引用类型,也可以说都是对象。

基本类型的值都是存在栈内存中的,引用类型得值是存在堆内存中的,因为引用类型的值可能很大,所以我们赋值的时候实际上只是一串存在栈内存中的指针。

基本数据类型的比较是值的比较

    var a = 1;
    var b = 1;
    console.log(a === b);//true

引用数据类型的比较是引用的比较。

    var a = [1,2,3];
    var b = [1,2,3];
    console.log(a == b); // false

赋值:传值与传址

当我们给基本数据类型赋值时,实际上是在传值,也就是在栈内存中开辟出一块新的内存存这个新变量,例如:

var a = 7;
var b = a;
a++;
console.log(a, b);  //8 7

基本数据类型的两个变量是两个互不影响的变量。

而当我们在给引用数据类型赋值时,实际上赋值的是指针地址。例如下面的代码:

var a = {name: 'wan'};
var b = a;
a.name = 'cheng';
console.log(a);  //{name: "cheng"}
console.log(b);  //{name: "cheng"}

由于a和b两个变量存的都是指向同一个对象的指针,所以这两个对象之间就能够相互影响了。

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant