js基本类型和引用类型

先来两个例题

//1.
var person;
person.age=10;
console.log(person.age)     //undefined person是字符串而不是对象,没有属性

//2.
var person= new Object();
person.age =20;
console.log(person.age);     //20

变量的引用和复制

//1.
var a = new Array();
foo ();
function foo(){
    var b=[];
    b[0]=1;
    b[1]=2;
    a=b;
    console.log(a)    //[1,2]
    b=[];
    console.log(a);    //[1,2]    因为上面a=b不是复制,而引用,a的指针指向数组[1,2],,即使b=[],a依然指向数组
}

注:虽然foo() 在 function foo(){}之前,但由于js解析时会自动将函数声明提前,所以foo()会在后面运行,所以可以正常显示

原文地址:https://www.cnblogs.com/keRee/p/6252694.html