zoukankan      html  css  js  c++  java
  • js创建map

    function Map() {
    var struct = function(key, value) {
    this.key = key;
    this.value = value;
    }

    var put = function(key, value){
    for (var i = 0; i < this.arr.length; i++) {
    if ( this.arr[i].key === key ) {
    this.arr[i].value = value;
    return;
    }
    }
    this.arr[this.arr.length] = new struct(key, value);
    }

    var get = function(key) {
    for (var i = 0; i < this.arr.length; i++) {
    if ( this.arr[i].key === key ) {
    return this.arr[i].value;
    }
    }
    return null;
    }

    var remove = function(key) {
    var v;
    for (var i = 0; i < this.arr.length; i++) {
    v = this.arr.pop();
    if ( v.key === key ) {
    continue;
    }
    this.arr.unshift(v);
    }
    }

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

    var isEmpty = function() {
    return this.arr.length <= 0;
    }
    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <script src="jquery-1.7.2.js" type="text/javascript"></script>

    将map封装成js引入
    <script src="map.js" type="text/javascript"></script>
    <script type="text/javascript">

    function test(){

    var map = new Map();
    map.put("re","redhacker");

    alert("mapValue"+ map.get("re"))

    }


    </script>

    <body>
    <form>

    <input type="button" value="取map值" onclick="test();"
    </form>
    </body>
    </html>

  • 相关阅读:
    SqlServer:创建索引
    SqlServer:使用视图 View
    SqlServer:修改和删除数据
    网络管理:SNMPv1
    《剑指 Offer》学习记录:题 9:用两个栈实现队列
    《剑指 Offer》学习记录:题 27:二叉树的镜像
    团队冲刺9
    团队冲刺8
    团队冲刺7
    团队冲刺6
  • 原文地址:https://www.cnblogs.com/yy123/p/4011601.html
Copyright © 2011-2022 走看看