Javascript代理:node-proxy,Harmony,带有魔法getter和setter的混合对象......在Coffeescript中?

时间:2011-10-15 06:44:48

标签: javascript node.js coffeescript ecmascript-harmony

我想有一个代理对象,附加方法和私有变量。

也就是说,所有正常的对象属性:

foo = {}
foo.bar = "baz"
foo.boo = "hoo"

有一些原型:

foo.setPrivateThings = function(value){ if (value) private = value; return private; }

其中枚举跳过私有变量/函数:

console.log(foo); // { bar: "baz", boo: "hoo" }

和get / set将通过魔术getter / setter运行:

foo.doesntexist = "..." = function(key){ console.log "Setting "+key; return new Date(); }

到目前为止,我有使用node-proxy的这个hokey Coffeescript。还有更好的答案吗?

class Data
    constructor: (obj) ->
        @proxy = require "node-proxy"

        p = @proxy.create
            has: (name) ->
                name of obj
            hasOwn: (name) ->
                ({}).hasOwnProperty.call obj, name
            get: (receiver, name) ->                
                p = obj.transform()[name]
                if typeof p != 'undefined' && p != '__lookupGetter__' && p != '__lookupSetter__'
                    return p()
                # We could do magic getting here
                obj[name]
            set: (receiver, name, val) ->
                # We could do magic setting here
                obj[name] = val
                true
            enumerate: ->
                result = []
                for name of obj
                    result.push name if typeof obj[name] != 'function'
                result
            keys: ->
                Object.keys obj
        , obj

        _transform = {}
        p.transform = (_t) ->
            _transform = _t if _t
            return _transform

        return p


d = new Data
    name: "Bill"
    colors: ["blue", "red"]

d.transform
    timer: ->
        return new Date()

console.log d.name, d.colors, d.timer

console.log d

产生

Bill [ 'blue', 'red' ] Sat, 15 Oct 2011 06:39:27 GMT
{ name: 'Bill', colors: [ 'blue', 'red' ] }

2 个答案:

答案 0 :(得分:3)

  

其中枚举跳过私有变量/函数:

Object.defineProperty(foo, "name", {
  get: function () { "return magic"; },
  set: function (value) { setMagic(value); },
  writable: true,
  configurable: true
  enumerable: false
});

代理是过度的,只需定义不可枚举的属性

答案 1 :(得分:0)

感谢@Raynos,我认为这个解决方案可能适合(将进行更多实验):

obj =
    firstname: "Bill"
    lastname: "Fell"
    colors: ["blue", "red"]

transform =
    name: ->
        "#{this.firstname} #{this.lastname}"
    colorstoo: "colors"

for key, val of transform   
    Object.defineProperty obj, key,
        get: if typeof val == "string" then new Function("return this.#{val}") else val
        enumerable: true

console.log obj.name, obj.colorstoo
console.log obj

产生

Bill Fell [ 'blue', 'red' ]

{ firstname: 'Bill',
  lastname: 'Fell',
  colors: [ 'blue', 'red' ],
  name: [Getter],
  colorstoo: [Getter] }

请注意,此解决方案不允许动态获取/设置,仅定义为列表。