var a = {age: 1};
var b = {age: 1};
console.log(a === b); //false
var ShoppingCar = (function() {
//这个是由购物车构造器所创建的实例
var instance;
//购物车的构造器函数
function Trolley() {
this.date = new Date().getDate(); //实例属性,当前日期
}
//原型属性,一个返回当前日期的方法
Trolley.prototype.getDate = function() {
return this.date;
};
//暴露出去的公共API
return function() {
//如果实例不存在,那么就调用Trolley构造器实例化一个
if(!instance) {
instance = new Trolley();
}
//将实例返回外部
return instance;
};
}());
var a = new ShoppingCar();
var b = new ShoppingCar();
console.log(a === b); //true
原文:http://www.cnblogs.com/yugege/p/5166956.html