首页 > Web开发 > 详细

Dictionary of js

时间:2017-11-08 13:00:03      阅读:223      评论:0      收藏:0      [点我收藏+]
function Dictionary() {
    this.datastore = new Array();
};
Dictionary.prototype = {
    constructor: Dictionary,
    add: function(key, value) {
        this.datastore[key] = value;
    },
    find: function(key) {
        return this.datastore[key];
    },
    remove: function(key) {
        delete this.datastore[key];
    },
    showAll: function() {
        var _this = this;
        Object.keys(this.datastore).sort().forEach(function(val, key) {
            console.log(val + " -> " + _this.datastore[val]);
        });
    },
    count: function() {
        var n = 0;
        Object.keys(this.datastore).forEach(function(val, key) {
            ++n;
        });
        return n;
    },
    clear: function() {
        var _this = this;
        Object.keys(this.datastore).forEach(function(val, key) {
             delete _this.datastore[val];
        });
    }
};
var telephoneBook = new Dictionary();
telephoneBook.add(‘Tom‘, 13713750081);
telephoneBook.add(‘Bom‘, 13713550081);
telephoneBook.add(‘Cindy‘, 43713750081);
telephoneBook.showAll();

  

Dictionary of js

原文:http://www.cnblogs.com/lemon-zhang/p/7803180.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!