Skip to content

Instantly share code, notes, and snippets.

@ccwq
Last active November 9, 2017 01:47
Show Gist options
  • Save ccwq/974ec63e0f4e9bda5fd0f3263c527fbb to your computer and use it in GitHub Desktop.
Save ccwq/974ec63e0f4e9bda5fd0f3263c527fbb to your computer and use it in GitHub Desktop.
对象池的基本实现
import _ from "lodash";
//marker对象池
let markerMgr = {
_idCounter:0,
_pool:[],
_outPool:{},
_reset(ins){
var m = this;
},
//归还
reback(ins){
var m = this;
m._reset(ins);
delete m._outPool[ins.$$$id];
m._pool.push(ins);
},
//借取
pick(){
var m = this;
var marker;
if(!m._pool.length) {
marker = new AMap.Marker();
marker.$$$id = m._idCounter++;
}else{
marker = m._pool.pop();
}
m._outPool[marker.$$$id] = marker;
return marker;
},
//归还所有
rebackAll(stepCallback){
var m = this;
_.each((ins,$id)=>{
debugger;
if(stepCallback) {
stepCallback(ins);
}
m.reback(ins);
})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment