Javascript Data Structure Dictionary via function

Description

Javascript Data Structure Dictionary via function



function Dictionary(){

    var items = {};

    this.set = function(key, value){
        items[key] = value; //{1}
    };/*from  w  ww.  j  a  v  a2s  .c om*/

    this.delete = function(key){
        if (this.has(key)){
            delete items[key];
            return true;
        }
        return false;
    };

    this.has = function(key){
        return items.hasOwnProperty(key);
        //return value in items;
    };

    this.get = function(key) {
        return this.has(key) ? items[key] : undefined;
    };

    this.clear = function(){
        items = {};
    };

    this.size = function(){
        return Object.keys(items).length;
    };

    this.keys = function(){
        return Object.keys(items);
    };

    this.values = function(){
        var values = [];
        for (var k in items) {
            if (this.has(k)) {
                values.push(items[k]);
            }
        }
        return values;
    };

    this.each = function(fn) {
        for (var k in items) {
            if (this.has(k)) {
                fn(k, items[k]);
            }
        }
    };

    this.getItems = function(){
        return items;
    }
}
var dictionary = new Dictionary();

dictionary.set('A', 'a@email.com');
dictionary.set('B', 'b@email.com');
dictionary.set('C', 'c@email.com');

console.log(dictionary.has('A'));   //outputs true
console.log(dictionary.size());   //outputs 3

console.log(dictionary.keys()); 
console.log(dictionary.values());
console.log(dictionary.get('B'));

dictionary.delete('C');

console.log(dictionary.keys()); 
console.log(dictionary.values()); 

console.log(dictionary.getItems()); 



PreviousNext

Related