definition(定义):
产生一个类的唯一实例,
实现方法:
先判断实例是否存在,如果存在直接返回,如果不存在创建了再返回,因此就确保了一个类只有一个实例对象。
一、实现的方法:
method 1 :
构造函数内部判断:
function Construct () {
//确保只有单例
if (Construct.unique !== undefined) {
return Construct.unique
}
// 其它代码
this.name = "NYF"
this.age = "24"
Construct.unique = this
}
var t1 = new Construct()
var t2 = new Construct()
也有 t1 === t2