A complete instance _javascript technique of Map Collection tool class implemented by JavaScript

Source: Internet
Author: User

This example describes the Map collection tool class for JavaScript implementations. Share to everyone for your reference. as follows:

var Map = function () {//construct Entry entity var Entry = function (key, value) {This.key = key;
  This.value = value;
  } this.entries = new Array (); Constructs the put method to place a entry This.put = function (key, value) {//array in the array does not put in for (var i = 0; i < this.size (); i++)
      {if (This.entries[i].key = = key) {return false;
  } this.entries.push (New Entry (key, value));
  }; Simulate Get method This.get = function (key) {for (var i = 0; i < this.size (); i++) {if (This.entries[i].key = = Ke
      Y) {return this.entries[i].value;
  } return null;
  };
    Lookup Subscript Value This.indexof = function (key) {var index =-1;
        for (var i = 0; i < this.size (); i++) {if (This.entries[i].key = = key) {index = i;
      Break
  } return index;
    //Delete an element this.remove = function (key) {var index = this.indexof (key);
    if (index!=-1) {This.entries.splice (index, 1); }//Get map length This.siZe = function () {return this.entries.length;
  };
    Reset key value to This.setvalue = function (key, value) {var index = this.indexof (key);
    if (index!=-1) {this.entries[i].value = value;
  };
  };
  is an empty map this.isempty = function () {return this.size () <= 0;
  };
  Empty map;
  This.clear = function () {this.entries = [];
  }; Get entry Entity This.getentry = function (index) {if (index >= 0 && Index < this.size ()) {return th
    Is.entries[index];
  return null;
    } this.tostring = function () {var str = ' [';
    for (var i = 0; i < this.size (); i++) {str + + this.getentry (i). Key + "=" + this.getentry (i). Value + ",";
    }//Remove the Last "," str = str.substring (0, str.length-1);
    str = + "]";
  return str;
};

 }

The

wants this article to help you with your JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.