• 用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();
      };
    })();
  • 相关阅读:
    【洛谷P6835】线形生物
    【洛谷P2679】子串
    【洛谷P5072】盼君勿忘
    【洛谷P3312】数表
    【洛谷P1447】能量采集
    【洛谷P2257】YY的GCD
    【洛谷P4318】完全平方数
    【AT2300】Snuke Line
    window.showModalDialog
    js typeof
  • 原文地址:https://www.cnblogs.com/zfc2201/p/3500236.html
Copyright © 2020-2023  润新知