构造函数四步走
- 函数内部建立一个私有域,悄悄的建立一个局部变量,是空对象{}
- 函数将自己的上下文设置成这个空对象{},也就是将函数中的this表示这个空对象
- 函数执行所有语句
- 所有的有语句执行完毕后,函数返回这个对象,也就是将自己的this返回
function GouZao (name) {
this.name = name
console.log(this);
}
GouZao('王二麻子') // this是window
var a = new GouZao('zhangsan') // new完后执行构造函数 this指向的是新生成的对象
console.log(a); // zhangsan