# 使用关键字new创建新实例对象经过了以下几步:
- 创建一个新的临时对象
- 将临时对象的_proto_(原型链)指向构造函数的 prototype(原型对象)
- 将构造函数的作用域赋值给新对象 (也就是this指向临时对象)
- 执行构造函数中的代码(为这个新对象添加属性)
- 返回临时对象
function Person (name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.sayName = function () {
return this.name;
};
}
var person = new Person("tom", 21, "man");
console.log(person.name);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13