3

I would like to have a proxied object with methods and private variables attached to it.

That is to say, all normal object properties:

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

with some prototypes:

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

where enumeration skips private variables/functions:

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

and get/set will be run through a magic getter/setter:

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

So far I have this hokey Coffeescript using node-proxy. Any better answers?

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

produces

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

2 Answers2

3

where enumeration skips private variables/functions:

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

Proxies are overkill, just define non-enumerable properties

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • Thank you, but I still need the magic get() for other uses— the example was just pared down for space. – Jareb Shubway Oct 15 '11 at 13:25
  • @JarebShubway defineProperty allows get and set. Also get and set are really expensive, why do you need them? – Raynos Oct 15 '11 at 13:28
  • I've got a complex set of data I want to modify as it's accessed, based on different rules. Starting with the example, a dynamic list of keys (in example, _transform) that represents some arbitrary rules (in example, return new Date()) to apply at access time. – Jareb Shubway Oct 15 '11 at 15:48
0

Thanks to @Raynos, I think this solution may be suitable (will experiment more):

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

produces:

Bill Fell [ 'blue', 'red' ]

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

note that this solution doesn't allow for dynamic get/set, only defined as a list.

  • [`get` must be a function or `undefined`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty). `if typeof val is "string" then get: -> this[val] else value: val`? – Ricardo Tomasi Oct 16 '11 at 00:16
  • Besides, what's the point of all this? loops are already guarded against prototypes via `.hasOwnProperty()`, `for own key, val of obj`, and you can have "privates" with a module pattern. – Ricardo Tomasi Oct 16 '11 at 00:24