You could emulate private by defining your data not as properties but as var
s in the constructor. However, this requires that your methods be defined in the instance (boundconstructor to this
)have access to them. This means that your methods are created on a per-instance basis, rather than onshared via the prototype, throwing away the concept of shared methods.
function LruCache(capacity){
this._capacity = capacity || 1;
this._accessMap = [];
this._items = {};
}
LruCache.prototype.cacheaddItem = function(key, value){
if(this._accessMap.length >= this._capacity) this.removeOldest();
this._accessMap.push(key);
this._items[key] = value;
};
LruCache.prototype.remove = function(key){
this._accessMap.splice(this._accessMap.indexOf(key), 1);
delete this._items[key];
};
LruCache.prototype.removeOldest = function(){
var oldestKey = this._accessMap.shift();
delete this._items[oldestKey];
};
LruCache.prototype.getCapacity = function(){
return this._capacity;
};
LruCache.prototype.getSize = function(){
return this._accessMap.length;
};
LruCache.prototype.getItems = function(){
var instance = this;
return Object.keys(this._items).reduce(function(carry, key){
carry[key] = instance._items[key];
return carry;
}, {});
};
function LruCache(capacity){
var _capacity = capacity || 1;
var _accessMap = [];
var _items = {};
function removeOldest(){
var oldestKey = _accessMap.shift();
delete _items[oldestKey];
};
this.cacheaddItem = function(key, value){
if(_accessMap.length >= _capacity) this.removeOldest();
_accessMap.push(key);
_items[key] = value;
};
this.remove = function(key){
_accessMap.splice(this._accessMap.indexOf(key), 1);
delete _items[key];
};
this.removeOldestgetCapacity = function(){
var oldestKey = _accessMap.shift();
deletereturn _items[oldestKey];_capacity;
};
this.getCapacitygetSize = function(){
return _capacity;_accessMap.length;
};
this.getSizegetItems = function(){
return _accessMapObject.length;keys(_items).reduce(function(carry, key){
carry[key] = _items[key];
return carry;
}, {});
};
}