function Parent() {
this.a = 1;
this.b = [1, 2, this.a];
this.c = {
demo: 5
};
this.show = function() {
console.log(this.a, this.b, this.c.demo);
}
this.change = function() {
this.a = this.b.length;
this.c.demo = this.a++;
}
}
var parent = new Parent();//
// var parent={a:4,b:[1,2,1],c:{demo:3},show:func,change:func}
parent.change();//
parent.show();//4 [1,2,1] 3
本题难点在哪?
首先是a 为什么等于4
在我们调用change()函数之后this.a = this.b.length;a的值已经等于b数组的长度了就是3
this.c.demo = this.a++; a++ 先取值 就取到3 然后a在自增变成4 但是b这个数组为什么不变呢
因为this.b = [1, 2, this.a]; 这个数组 this.a在最开始代码运行的时候已经固定了是1 this.a不是一个引用数据 他不会变化 除非后面有代码去更新了b的值