SoFunction
Updated on 2025-03-06

JavaScript implementation of Dictionary example basics

cause

Recently, I was reading "Data Structures and Algorithms--Javascript Description", and then searched, hoping to find a suitable library to reference and record it so that it can be used as soon as possible in the future. At the end, I didn't find that it was very suitable for me, so I decided to implement it one by one.

Programming ideas

The bare object datastore is used for element storage;

Two methods for obtaining dictionary length are implemented, one is variable tracking and the other is real-time calculation.

Your own implementation

(function(){
    "use strict";
    function Dictionary(){
        this._size = 0;
         = (null);
    }
     = function(){
        return this._size === 0;
    };
     = function(){
        return this._size;
    };
     = function(){
        for(var key in ){
            delete [key];
        }
        this._size = 0;
    };
     = function(key, value){
        [key] = value;
        this._size++;
    };
     = function(key){
        return [key];
    };
     = function(){
        var n = 0;
        for(var key in ){
            n++;
        }
        return n;
    };
     = function(key){
        delete [key];
        this._size--;
    };
     = function(){
        for(var key in ){
            (key + "->" + [key]);
        }
    };
     = Dictionary;
})();

Source code address

/zhoutk/js-data-struct 

/zhoutk/jsDataStructs 

The above is the detailed content of the basic example of the JavaScript implementation of the Dictionary Dictionary. For more information about the JavaScript Dictionary Dictionary, please pay attention to my other related articles!