

3. new 命令
3.1 基本用法
3.1.1 new 的基本用法
new 命令的作用,就是执行构造函数,返回一个实例对象。
var Vehicle = function () {
this.price = 1000;
};
var v = new Vehicle();
v.price // 1000上面代码通过 new 命令,让构造函数 Vehicle 生成一个实例对象,保存在变量 v 中。 new 命令执行时,构造函数内部的 this ,就代表了新生成的实例对象, this.price 表示实例对象有一个 price 属性,值是1000。
下面两行代码是等价的,但是为了表示这里是函数调用, 推荐使用括号 。
// 推荐的写法 var v = new Vehicle(); // 不推荐的写法 var v = new Vehicle;
3.1.2 保证构造函数必须与 new 命令一起使用
如果忘了使用 new 命令,直接调用构造函数会发生什么事?
var Vehicle = function (){
this.price = 1000;
};
var v = Vehicle();
v // undefined
price // 1000上面代码中,调用 Vehicle 构造函数时,忘了加上 new 命令。结果,变量 v 变成了 undefined ,而 price 属性变成了全局变量。因此,应该非常小心,避免不使用 new 命令、直接调用构造函数。
为了保证构造函数必须与 new 命令一起使用,有2种解决办法:
构造函数内部使用严格模式,即第一行加上 use strict 。 这样的话,一旦忘了使用 new 命令,直接调用构造函数就会报错。
function Fubar(foo, bar){
'use strict';
this._foo = foo;
this._bar = bar;
}
Fubar()
// TypeError: Cannot set property '_foo' of undefined上面代码的 Fubar 为构造函数, use strict 命令保证了该函数在严格模式下运行。由于严格模式中,函数内部的 this 不能指向全局对象,默认等于 undefined ,导致不加 new 调用会报错(JavaScript 不允许对 undefined 添加属性)。
构造函数内部判断是否使用 new 命令,如果发现没有使用,则直接返回一个实例对象。
function Fubar(foo, bar) {
if (!(this instanceof Fubar)) {
return new Fubar(foo, bar);
}
this._foo = foo;
this._bar = bar;
}
Fubar(1, 2)._foo // 1
(new Fubar(1, 2))._foo // 13.2 new 命令的原理
使用 new 命令时,它后面的函数依次执行下面的步骤。
prototype
this
也就是说,构造函数内部, this 指的是一个新生成的空对象,所有针对 this 的操作,都会发生在这个空对象上。构造函数之所以叫“构造函数”,就是说这个函数的目的,就是操作一个空对象(即 this 对象),将其“构造”为需要的样子。
3.2.1 关于 return 语句
如果构造函数内部有 return 语句,而且 return 后面跟着一个对象, new 命令会返回 return 语句指定的对象;否则,就会不管 return 语句,返回 this 对象。
var Vehicle = function () {
this.price = 1000;
return 1000;
};
(new Vehicle()) === 1000
// false new命令忽略了这个 return 语句,返回“构造”后的 this 对象。如果 return 语句返回的是一个跟 this 无关的新对象, new 命令会返回这个新对象,而不是 this 对象。
var Vehicle = function (){
this.price = 1000;
return { price: 2000 };
};
(new Vehicle()).price
// 2000 new 命令返回这个对象,而不是 this 对象。3.2.2 对普通函数使用 new
如果对普通函数(内部没有 this 关键字的函数)使用 new 命令,则会 返回一个空对象 。
function getMessage() {
return 'this is a message';
}
var msg = new getMessage();
msg // {}
typeof msg // "object"这是因为 new 命令总是返回一个对象,要么是实例对象,要么是 return 语句指定的对象。本例中, return 语句返回的是字符串,所以 new 命令就忽略了该语句。
