JavaScript實現Java中的Map結構

來源:互聯網
上載者:User

Java中的Map是一個很實用的集合,習慣了使用Java中的Map,換別的語言沒Map時感覺很不爽,以前寫Flex AS代碼時碰到過用要用Map的情況,不過AS其實有Dictionary字典類可以代替Java中的Map,同時也可以使用對象的屬性-值形式來實現Map,在這裡JS的Map實現就是使用的對象的屬性-值。實現很簡單,這裡只是為了讓Java程式員輕鬆的編寫JS代碼。

//constructionfunction Map() {        this.obj = new Object();};//add a key-valueMap.prototype.put = function(key, value) {        this.obj[key] = value;};//get a value by a key,if don't exist,return undefinedMap.prototype.get = function(key) {        return this.obj[key];};//remove a value by a keyMap.prototype.remove = function(key) {        if(this.get(key)==undefined) {                return;        }        delete this.obj[key];};//clear the mapMap.prototype.clear = function() {        this.obj = new Object();};//get the sizeMap.prototype.size = function() {        var ary = this.keys();        return ary.length;};//get all keysMap.prototype.keys = function() {        var ary = new Array();        for(var temp in this.obj) {                ary.push(temp);        }        return ary;};//get all valuesMap.prototype.values = function() {        var ary = new Array();        for(var temp in this.obj) {                ary.push(this.obj[temp]);        }        return ary;};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.