var SingletonTester = (function () {
//参数:传递给单例的一个参数集合
function Singleton(args) {
//设置args变量为接收的参数或者为空(如果没有提供的话)
var args = args || {};
//设置name参数
this.name = 'SingletonTester';
//设置pointX的值
this.pointX = args.pointX || 6; //从接收的参数里获取,或者设置为默认值
//设置pointY的值
this.pointY = args.pointY || 10;
}
//实例容器
var instance;
var _static = {
name: 'SingletonTester',
//获取实例的方法
//返回Singleton的实例
getInstance: function (args) {
if (instance === undefined) {
instance = new Singleton(args);
}
return instance;
}
};
return _static;
})();
var signleOne = SingletonTester.getInstance({pointX: 7});
var signleTwo = SingletonTester.getInstance({pointY: 20});
console.log(signleOne === signleTwo) //true
console.log(signleOne.pointX) //7
console.log(signleTwo.pointY) //10
问题是:signleTwo 的SingletonTester再次调用getInstance的时候instance不是undefined,也就是走到了 `return instance`这一步?每次调用getInstance不是应该instance都是undefined嚒?
单例如果都不一样了,还能叫单例么!