Object.prototype.link
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.)
I'd like to use link(obj, target).
E.g. a = 10; link(b, a); a++; b++; print(b); // output: 12
memolus at googlemail.com wrote:
I'd like to use link(obj, target).
E.g. a = 10; link(b, a); a++; b++; print(b); // output: 12
That would require a "catchall" mechanism, allowing accesses to nonexistent properties of an object to be handled. It's quite likely that such a mechanism will be added, I think, but there is currently no detailed concrete proposal.
Some previous discussion: esdiscuss/2008-November/008159
On Sat, 21 Feb 2009 18:52:06 +0100, memolus at googlemail.com <memolus at googlemail.com> wrote:
I'd like to use link(obj, target).
E.g. a = 10; link(b, a); a++; b++; print(b); // output: 12
Here you are not linking properties of objects, or values at all, as your first post seemed to suggest. Instead your are linking variables (aliasing).
I.e, what you ask for is for link(b,a) to create a new variable, "b", aliasing the existing variable "a". Is this correct?
David-Sarah Hopwood wrote:
memolus at googlemail.com wrote:
I'd like to use link(obj, target).
E.g. a = 10; link(b, a); a++; b++; print(b); // output: 12
That would require a "catchall" mechanism, allowing accesses to nonexistent properties of an object to be handled. It's quite likely that such a mechanism will be added, I think, but there is currently no detailed concrete proposal.
Sorry, I misread your example; a catchall mechanism is not particularly relevant to it.
What you've asked for above can be implemented in terms of "linking" of properties using getters/setters, in the case where the variables are in the global scope or in a scope introduced using 'with'. However, why on earth would you want this? It looks frightful.
On Sun, Feb 22, 2009 at 6:01 AM, David-Sarah Hopwood <david.hopwood at industrial-designers.co.uk> wrote:
David-Sarah Hopwood wrote:
memolus at googlemail.com wrote:
I'd like to use link(obj, target).
E.g. a = 10; link(b, a); a++; b++; print(b); // output: 12
That would require a "catchall" mechanism, allowing accesses to nonexistent properties of an object to be handled. It's quite likely that such a mechanism will be added, I think, but there is currently no detailed concrete proposal.
Sorry, I misread your example; a catchall mechanism is not particularly relevant to it.
What you've asked for above can be implemented in terms of "linking" of properties using getters/setters, in the case where the variables are in the global scope or in a scope introduced using 'with'. However, why on earth would you want this? It looks frightful.
I agree.
BTW Several implementations implement a String.prototype.link method.
('a').link('b');
=> "<a href="b">a</a>"
:-)
Garrett
a and b are properties of object "this" (the global object). It has to work the same way like linking "x.a" to "y.b". link(this, "a", this, "b") does work with the function of David-Sarah Hopwood.
2009/2/22 Lasse R.H. Nielsen <atwork at infimum.dk>:
Can you describe this in terms of language features from another
language? For example, the use-case you've provided could be
accomplished with pointers in C. In C++ you could create a
MutableNumber class and use operator overloading. Is that the kind of
thing you're looking for?
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;}); }