只能说,根据用法,模拟出的它构造函数,方法有点想JQ,
var cacheFactory=function (id){
return new cacheFactory.prototype.init(id);
}
cacheFactory.prototype.init=function(id){
this.id=id;
this.size=0;
this.content={};
return this;
}
cacheFactory.prototype.init.prototype = cacheFactory.prototype ;
cacheFactory.prototype.put = function(name,value){
this.content[name]=value;
this.size++;
}
cacheFactory.prototype.get=function(name){
return this.content[name];
}
cacheFactory.prototype.info=function(){
return this;
}
var m1 = cacheFactory('mydata');
m1.put('name','magicfly');
m1.put('age',222);
console.log(m1.info())
后面感觉用PROTOTYPE实现,太臃肿,用单体实现比较简单
于是有了下面代码
var cacheFactory=function (id){
var self ={
id:id,
size:0,
put:function(name,value){
content[name]=value;
self.size++;
},
get:function(name){
return content[name];
},
info:function(){
return self;
}
}
return self;
}