Skip to main content
deleted 5 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
this._items = [];

I'm pretty sure you wanted a hash {} instead of an array.

this._items[key] = this[key] = value;
function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  function removeOldest(){
    var oldestKey = _accessMap.shift();
    delete _items[oldestKey];
  };

  this.addItem = function(key, value){
    if(_accessMap.length >= _capacity) removeOldest();
    _accessMap.push(key);
    _items[key] = value;
  };

  this.remove = function(key){
    _accessMap.splice(this._accessMap.indexOf(key), 1);
    delete _items[key];
  };

  this.getCapacity = function(){
    return _capacity;
  };

  this.getSize = function(){
    return _accessMap.length;
  };

  this.getItems = function(){
    return Object.keys(_items).reduce(function(carry, key){
      carry[key] = _items[key];
      return carry;
    }, {});
  };

}
this._items[key] = this[key] = value;
function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  function removeOldest(){
    var oldestKey = _accessMap.shift();
    delete _items[oldestKey];
  };

  this.addItem = function(key, value){
    if(_accessMap.length >= _capacity) removeOldest();
    _accessMap.push(key);
    _items[key] = value;
  };

  this.remove = function(key){
    _accessMap.splice(this._accessMap.indexOf(key), 1);
    delete _items[key];
  };

  this.getCapacity = function(){
    return _capacity;
  };

  this.getSize = function(){
    return _accessMap.length;
  };

  this.getItems = function(){
    return Object.keys(_items).reduce(function(carry, key){
      carry[key] = _items[key];
      return carry;
    }, {});
  };

}
this._items = [];

I'm pretty sure you wanted a hash {} instead of an array.

this._items[key] = this[key] = value;
function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  function removeOldest(){
    var oldestKey = _accessMap.shift();
    delete _items[oldestKey];
  };

  this.addItem = function(key, value){
    if(_accessMap.length >= _capacity) removeOldest();
    _accessMap.push(key);
    _items[key] = value;
  };

  this.remove = function(key){
    _accessMap.splice(_accessMap.indexOf(key), 1);
    delete _items[key];
  };

  this.getCapacity = function(){
    return _capacity;
  };

  this.getSize = function(){
    return _accessMap.length;
  };

  this.getItems = function(){
    return Object.keys(_items).reduce(function(carry, key){
      carry[key] = _items[key];
      return carry;
    }, {});
  };

}
added 51 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37

You could emulate private by defining your data not as properties but as vars 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;
    }, {});
  };

}

You could emulate private by defining your data not as properties but as vars in the constructor. However, this requires that your methods be in the instance (bound to this) rather than on the prototype, throwing away the concept of shared methods.

function LruCache(capacity){
  this._capacity = capacity || 1;
  this._accessMap = [];
  this._items = {};
}

LruCache.prototype.cache = 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;
};
function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  this.cache = 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.removeOldest = function(){
    var oldestKey = _accessMap.shift();
    delete _items[oldestKey];
  };

  this.getCapacity = function(){
    return _capacity;
  };

  this.getSize = function(){
    return _accessMap.length;
  };
}

You could emulate private by defining your data not as properties but as vars in the constructor. However, this requires that your methods be defined in the constructor to have access to them. This means that your methods are created on a per-instance basis, rather than shared via the prototype.

function LruCache(capacity){
  this._capacity = capacity || 1;
  this._accessMap = [];
  this._items = {};
}

LruCache.prototype.addItem = 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.addItem = function(key, value){
    if(_accessMap.length >= _capacity) removeOldest();
    _accessMap.push(key);
    _items[key] = value;
  };

  this.remove = function(key){
    _accessMap.splice(this._accessMap.indexOf(key), 1);
    delete _items[key];
  };

  this.getCapacity = function(){
    return _capacity;
  };

  this.getSize = function(){
    return _accessMap.length;
  };

  this.getItems = function(){
    return Object.keys(_items).reduce(function(carry, key){
      carry[key] = _items[key];
      return carry;
    }, {});
  };

}
added 51 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37

JavaScript ain't classical OOP. Don't force it to be like one. The default inheritance pattern in JS is prototypal, and everything is public and methods are shared across instances. 

You could define the methods insideemulate private by defining your data not as properties but as vars in the constructor to get. However, this requires that your methods be in the "public accessing private"instance (like you just didbound to this). But that defeats rather than on the purposeprototype, throwing away the concept of prototypal inheritance/shared methods approach as each method is created per instance and not shared methods.

function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  this.cache = function(key, value){
    if(this._accessMap.length >= this._capacity) this.removeOldest();
    this._accessMap.push(key);
    this._items[key] = value;
  };

  this.remove = function(key){
    this._accessMap.splice(this._accessMap.indexOf(key), 1);
    delete this._items[key];
  };

  this.removeOldest = function(){
    var oldestKey = this._accessMap.shift();
    delete this._items[oldestKey];
  };

  this.getCapacity = function(){
    return this._capacity;
  };

  this.getSize = function(){
    return this._accessMap.length;
  };
}

JavaScript ain't classical OOP. Don't force it to be like one. The default inheritance pattern in JS is prototypal, and everything is public. You could define the methods inside the constructor to get the "public accessing private" (like you just did). But that defeats the purpose of prototypal inheritance/shared methods approach as each method is created per instance and not shared.

function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  this.cache = function(key, value){
    if(this._accessMap.length >= this._capacity) this.removeOldest();
    this._accessMap.push(key);
    this._items[key] = value;
  };

  this.remove = function(key){
    this._accessMap.splice(this._accessMap.indexOf(key), 1);
    delete this._items[key];
  };

  this.removeOldest = function(){
    var oldestKey = this._accessMap.shift();
    delete this._items[oldestKey];
  };

  this.getCapacity = function(){
    return this._capacity;
  };

  this.getSize = function(){
    return this._accessMap.length;
  };
}

JavaScript ain't classical OOP. Don't force it to be like one. The default inheritance pattern in JS is prototypal, and everything is public and methods are shared across instances. 

You could emulate private by defining your data not as properties but as vars in the constructor. However, this requires that your methods be in the instance (bound to this) rather than on the prototype, throwing away the concept of shared methods.

function LruCache(capacity){
  var _capacity = capacity || 1;
  var _accessMap = [];
  var _items = {};

  this.cache = 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.removeOldest = function(){
    var oldestKey = _accessMap.shift();
    delete _items[oldestKey];
  };

  this.getCapacity = function(){
    return _capacity;
  };

  this.getSize = function(){
    return _accessMap.length;
  };
}
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
Loading