构造函数
构造函数理论上讲也是常规函数,它有以下两个约定:
(1)构造函数必须以大写字母开头;
(2)它们只有在new操作之后才被执行;
这里有个例子:
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name); // Jack
alert(user.isAdmin); // false
当显式使用new操作后,构造函数User(name)按下面的规则执行:
(1)创建一个空的对象,把this与这个空对象绑定;
(2)执行函数体,修改这个this,也就是把属性或方法添加到这个this里面;
(3)返回this;
就像下面这个例子:
function User(name) {
// this = {}; (implicitly)
// add properties to this
this.name = name;
this.isAdmin = false;
// return this; (implicitly)
}
双重语法构造器:new.target
在构造函数里面,new.target属性用来检测对象是否通过new来创建,如果是new创建的话,则new.target的值为函数体,否则为undefined,例如:function User() {
alert(new.target);
}
// without new:
User(); // undefined
// with new:
new User(); // function User { ... }
通常new.target在一些库的代码里使用得比较多,它可以使代码变得更加灵活,例如:
function User(name) {
if (!new.target) { // if you run me without new
return new User(name); // ...I will add new for you
}
this.name = name;
}
let john = User("John"); // redirects call to new User
alert(john.name); // John
构造函数的返回值
通常情况下,构造函数默认返回this,但是我们可以修改它的返回值。Javascript对构造函数的返回值设置了以下的规则:
(1)如果返回的是对象,则this会被覆盖;
(2)如果返回的是原型,则该原型会被忽略,结果还是返回this;
返回对象:
function BigUser() {
this.name = "John";
return { name: "Godzilla" }; // <-- returns an object
}
alert( new BigUser().name ); // Godzilla, got that object ^^
返回原型:
function SmallUser() {
this.name = "John";
return; // finishes the execution, returns this
// ...
}
alert( new SmallUser().name ); // John
ps:
使用new创建对象的时候,我们也可以省略圆括号,例如:
let user = new User; // <-- no parentheses
// same as
let user = new User();
不过这并不是一种好习惯。。。
构造函数方法:
我们不仅可以往构造函数添加属性,也可以添加方法,例如:
function User(name) {
this.name = name;
this.sayHi = function() {
alert( "My name is: " + this.name );
};
}
let john = new User("John");
john.sayHi(); // My name is: John
/*
john = {
name: "John",
sayHi: function() { ... }
}
*/