JavaScript uses arrays to implement key value pairs __java

Source: Internet
Author: User
Turn from: http://jsj.52zxw.net/article_view.php/99.html
VBS has a Dictionary object, JS also has the corresponding object, can JS Dictionary object is not cross-browser, so use the array to achieve the function of key value pairs.

Today to write browser-side JS program, you need to use the function of key-value pairs, VBS has dictionary objects, JS should also have a corresponding Dictionary object, check the JS manual, JS really have Dictionary object, program written, cross-browser a test, Found only IE support new ActiveXObject ("Scripting.Dictionary"), other browsers may have compatible writing, support Dictionary objects, online search for a while, did not search, suddenly think of JS Array, looked up some information, Realized I want the function, cross-browser test, Google, Firefox, Opera are compatible, the following is the program code, referring to an online example, slightly modified:

<script type= "Text/javascript" language= "JavaScript"
//Custom Dictionary object
function Dictionary () {
  This.data = new Array ();
 
 this.put = function (key,value) {
  This.data[key] = value;
 

 this.get = function (key) {
  return this.data[key];
 

 this.remove = function (key) {
  this.data[key] = null;
 
 
 this.isempty = function () {
  return this.data.length = 0;
 

 this.size = function () {
  return this.data.length;
 
}

//Use example
var d = new Dictionary ();
D.put ("CN", "country");
D.put ("US", "America");
document.write (d. Get ("CN"));
</script>

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.