function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.sayName = function () {
console.log(this.name)
}
}
let person1 = new Person("小明", 18, "男");
以上述的方式调用构造函数会经历以下四个步骤:
1、创建一个新对象;
2、将构造函数的作用域赋给新对象(因此this就指向了这个新对象);
3、执行构造函数中的代码(为这个新对象添加属性);
4、返回对象