David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Sat Feb 21 06:02:06 PST 2009

memolus at googlemail.com wrote:
> I'd like to see a feature in ECMAScript to link one object to another.
> example implementation:
> Object.prototype.link = function (prop, target, tprop) {
>     this.__defineGetter__(prop, function () {return target[tprop];});
>     this.__defineSetter__(prop, function (x) {target[tprop] = x;return x;});
> }

Your wish has been granted. This can be implemented in ES3.1 as follows:

  function link(obj, prop, target, tprop) {
    Object.defineProperty(obj, prop,
      {get: function() { return target[tprop]; },
       set: function(x) { target[tprop] = x; },
       enumerable: true

(Don't add methods to Object.prototype; it's not necessary, and
Object.prototype is a global namespace that is subject to conflicts.)

David-Sarah Hopwood ⚥

More information about the Es-discuss mailing list