Javascript模式(一) 单例模式

	function A(){
		// 存储实例对象
		var instance;
		// 重写构造函数,只返回闭包内的局部变量instance
		A = function(){
			return instance;
		}
		// 重写原型为实例本身 之后定义在原型上的属性和方法会直接赋在该实例上
		A.prototype = this;
		// 实例化
		instance = new A();
		// 重写构造函数
		instance.constructor = A;
		// 第一次实例化时返回重写之后的构造函数的实例
		return instance;
	}

	var a = new A();
	var b = new A();
	console.log(a == b); // true
	A.prototype.xx = "1212";
	console.log(a.xx, b.xx); // 1212 1212

  

原文地址:https://www.cnblogs.com/mr189/p/3923182.html