(1)构造函数内部原理
- 1、在函数体最前面隐式的加上this = {};
- 2、执行 this.xxx = xxx;
- 3、隐式的返回this
(2)实例化原理
new关键字实例化对象,改变this指向,由window到实例化出的对象本身
(3)代码示例
function Car(color, brand) {// var this = {// color: color,// brand: brand// }this.color = color;this.brand = brand;// 隐式返回// return this;// 可以显示返回引用值: {},[],function// var obj = {// color: 'red'// }// return obj;// 返回原始值无效// return '123';
}
// 实例化构造函数: new 作用就是造出this
var car1 = new Car('red', 'Benz');
console.log(car1.color);