編碼實現js的Set

來源:互聯網
上載者:User

標籤:ret   move   ase   strong   value   實現   string   arguments   log   

 

編碼實現js的Set

 

 1 /** 2 * 3 * Set 4 * 5 * @author: 阿寧 6 * 7 * @date:   2017年5月30日 8 * 9 */10 (function(global){11 12     function Set(){13         this.values = {};14         this.n = 0;15         this.add.apply(this,arguments);16     }17 18     Set.prototype.add = function(){19         for(var i = 0; i < arguments.length; i++){20             var val = arguments[i];21             var key = Set._v2s(val);22             if(!this.values.hasOwnProperty(key)){23                 this.values[key] = val;24                 this.n++;25             }26         }27         return this;28     };29 30     Set.prototype.remove = function(){31         for(var i = 0; i < arguments.length; i++){32             var key = Set._v2s(arguments[i]);33             if(this.values.hasOwnProperty(key)){34                 delete this.values[key];35                 this.n--;36             }37         }38         return this;39     };40 41     Set.prototype.contain = function(val){42         return this.values.hasOwnProperty(Set._v2s(val));43     };44 45     Set.prototype.foreach = function(f,c){46         for(var key in this.values){47             if(this.values.hasOwnProperty(key)){48                 f.call(c,this.values[key]);49             }50         }51         return this;52     };53 54     Set._v2s = function(val){55         switch(val){56             case undefined:return "u";57             case null:return "n";58             case true:return "t";59             case false: return "f";60             default:switch(typeof val){61                 case "number":return "#" + val;62                 case "string":return "\"" + val;63                 default : return "@" + objectId(val);64             }65         }66         function objectId(val){67             var prop = "|**objectid**|";68             if(!val.hasOwnProperty(prop)){69                 val[prop] = Set._v2s.next++;70             }71             return val[prop];72         }73     };74 75     Set._v2s.next = 100;76 77     global.Set = Set;78 79 })(this);

 

編碼實現js的Set

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.