• 用coffeescript实现类java的Map类


    class Map
        constructor : -> 
            @entry = {}
            @count = 0
        
        size : -> 
            return @count
        
        isEmpty : -> 
            return @count == 0
        
        containsKey : (key) ->
            if @isEmpty()
                return false
            return @entry.hasOwnProperty key
        
        containsValue : (val)->
            if @isEmpty()
                return false
            for key,_val of @entry
                if _val == val
                    return true
            return false
        
        get : (key)->
            if @isEmpty()
                return null
            if @containsKey key
                return @entry[key]     
            return null
        
        put : (key, val)->
            if !@entry.hasOwnProperty key
                @count += 1;  
            @entry[key] = val
            return @
        
        remove : (key)-> 
            if @isEmpty()
                return false
            if @containsKey key
                delete @entry[key]
                @count -= 1
                return true
            return false    
        
        putAll : (map)->  
            if !map instanceof Map
                return false
            entry = map.entry
            for key,val of entry
                @put(key, val)
            return true
        
        clear : ->
            @entry = {}
            @count = 0
            return @ 
        
        values : ->  
            vals = []
            for key,val of @entry
                vals.push val
            return vals
            
        keySet : ->  
            keys = []
            for key,val of @entry
                keys.push key
            return keys
        
        entrySet : ->  
            return @entry
        
        toString : ->  
            if typeof JSON == "undefined"
                throw new Error "JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+)."
            return JSON.stringify @entry
        
        valueOf : ->  
            return @toString()

     使用coffeescript编译后生成代码:

    (function() {
      var Map;
      var __hasProp = Object.prototype.hasOwnProperty;
      Map = function() {
        this.entry = {};
        this.count = 0;
        return this;
      };
      Map.prototype.size = function() {
        return this.count;
      };
      Map.prototype.isEmpty = function() {
        return this.count === 0;
      };
      Map.prototype.containsKey = function(key) {
        if (this.isEmpty()) {
          return false;
        }
        return this.entry.hasOwnProperty(key);
      };
      Map.prototype.containsValue = function(val) {
        var _a, _val, key;
        if (this.isEmpty()) {
          return false;
        }
        _a = this.entry;
        for (key in _a) {
          if (!__hasProp.call(_a, key)) continue;
          _val = _a[key];
          if (_val === val) {
            return true;
          }
        }
        return false;
      };
      Map.prototype.get = function(key) {
        if (this.isEmpty()) {
          return null;
        }
        if (this.containsKey(key)) {
          return this.entry[key];
        }
        return null;
      };
      Map.prototype.put = function(key, val) {
        if (!this.entry.hasOwnProperty(key)) {
          this.count += 1;
        };
        this.entry[key] = val;
        return this;
      };
      Map.prototype.remove = function(key) {
        if (this.isEmpty()) {
          return false;
        }
        if (this.containsKey(key)) {
          delete this.entry[key];
          this.count -= 1;
          return true;
        }
        return false;
      };
      Map.prototype.putAll = function(map) {
        var _a, entry, key, val;
        if (!map instanceof Map) {
          return false;
        }
        entry = map.entry;
        _a = entry;
        for (key in _a) {
          if (!__hasProp.call(_a, key)) continue;
          val = _a[key];
          this.put(key, val);
        }
        return true;
      };
      Map.prototype.clear = function() {
        this.entry = {};
        this.count = 0;
        return this;
      };
      Map.prototype.values = function() {
        var _a, key, val, vals;
        vals = [];
        _a = this.entry;
        for (key in _a) {
          if (!__hasProp.call(_a, key)) continue;
          val = _a[key];
          vals.push(val);
        }
        return vals;
      };
      Map.prototype.keySet = function() {
        var _a, key, keys, val;
        keys = [];
        _a = this.entry;
        for (key in _a) {
          if (!__hasProp.call(_a, key)) continue;
          val = _a[key];
          keys.push(key);
        }
        return keys;
      };
      Map.prototype.entrySet = function() {
        return this.entry;
      };
      Map.prototype.toString = function() {
        if (typeof JSON === "undefined") {
          throw new Error("JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+).");
        }
        return JSON.stringify(this.entry);
      };
      Map.prototype.valueOf = function() {
        return this.toString();
      };
    })();
  • 相关阅读:
    Centos7 ping 未知的名称或服务 DNS 配置问题
    实战项目用winform动手写一个计算器
    Element UI实现树形控件单选
    C#中定时服务
    Method breakpoints may dramatically slow down debugging 解决 项目无法启动 打开Breakpoints面板(快捷键:Ctrl Shift F8 ),将断点取消勾选即可。
    1. 查看NGINX是否在运行. 四种方式 nginx的配置文件的路径: /etc/nginx/nginx.conf
    文件上传 (2)
    访问关系图
    时间和错误
    Neuron Newsletter 202207|新增非 A11 驱动、即将支持 OPC DA
  • 原文地址:https://www.cnblogs.com/zfc2201/p/3500236.html
Copyright © 2020-2023  润新知