1
2
3
4
5
6
7
8
function Student(name) {
console.log(this); // Student {}
this.name = name; // Student {name : "codereasy"}
}

// 通过new关键字创建一个新对象的步骤是什么、构造函数是如何创建新对象的?
var xiaoming = new Student("codereasy")
console.log(xiaoming);

在构造函数中添加return,如果return的是对象,则直接返回该对象,如果return的是基本类型,则return语句无效,仍然返回我们创建的新对象。