建议构造函数的名称首字母大写。构造函数有两个显著特点:
使用 new 命令调用构造函数,创建实例,并返回这个对象。
function Point(x, y) {
// 构造函数
if (!(this instanceof Point)) return new Point(x, y);
this.x = x; //私有属性
this.y = y; // 私有属性
this.sum = function () {
// 方法 return this.x +
this.y;
};
}
var p1 = Point(100, 200); // 实例化对象1
var p2 = new Point(300, 400); // 实例化对象2
console.log(p1.x); // 100
console.log(p2.x); // 300
console.log(p1.sum()); // 300
console.log(p2.sum()); // 700
若果不使用 new ,直接使用小括号调用构造函数,这时的构造函数就是普通的函数, this 指向调用函数的对象,即客户端指代全局的 window 。
构造函数允许返回值,如果返回值为简单值,者被忽略,直接返回 this 指代的实例对象;如果返回值为对象,这回覆盖 this 指代的实例,返回 return 后面的对象。
这与 new 的解析过程有关:
function Point(x, y) {
// 构造函数
this.x = x; // 私有属性
this.y = y; // 私有属性
return {
x: true,
y: false,
};
}
var p1 = new Point(100, 200); // 实例化对象1
console.log(p1.x); // true
console.log(p1.y); // false
在普通函数内,可以通过 arguments.callee 引用自身。若果在严格模式下,不允许使用 arguments.callee 引用函数。这时需要用 new.target 来访问构造器。
function Point(x, y) {
// 构造函数
'use strict'; // 启用严格模式
if (!(this instanceof new.target)) return new new.target(x, y); // 检测 this 是否为实例对象
this.x = x; // 私有属性
this.y = y; // 私有属性
this.sum = function () {
// 方法
return this.x + this.y;
};
}
var p1 = new Point(100, 200); // 实例化对象1
console.log(p1.x); // 100
console.log(p1.sum()); // 300