~ Anders
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>
http://ejohn.org/blog/ecmascript-5-objects-and-properties/
As far as I know, v8 has reasonably good ES5 object and property support. Last time I checked, though, they had one bug that may or may not apply to you:
Let's say you have a native object (say a DOM element in a web browser) and it has a property on it with an internal setter. For example, "innerHTML."
As you may know, you can do myDomElement.innerHTML = someString, and it will update the innards of the element with the browser-evaluated string. Clearly there's magic happening here beyond just setting a property. The problem is that if you try to get a pointer that getter or setter method in v8, it will return undefined:
var d = Object.getOwnPropertyDescriptor(myDomElement, "innerHTML");
d.getter // undefined
d.setter // undefined
This makes it impossible to intercept native property assignments. If you wanted to toss an event any time content was assigned to innerHTML, you would be unable in v8.
Testing in firefox indicates that its JS engine *does* hand back a pointer to a native function. In that browser it's possible to shim property assignments.
Last I checked, bugs filed about this get flagged as "not a bug."
Nick
____________________
Nicholas Husher
nhu...@gmail.com
http://nickol.us
What he's asking for is a generic "any time I write or read any random
property, run this code" kind of thing. That's proxies. You can't do
that with Object.defineProperty, because you don't know what
properties you're going to be defining ahead of time.
It is my sincere hope that such a thing never makes it into JavaScript
proper. You can, of course, implement it as a userland module if you
do some C++ hacking, and the programs that use it will likely not be
portable to any other platform. But, your ORMs and such can be really
cool.
I investigated doing something like this as a way to have an object
that looks just like a regular object, but in fact saves all its data
to a redis or couchdb datastore. I abandoned it eventually, in favor
of explicit .get(k) and .set(k,v) functions because:
1. it's not so bad.
2. o.set("foo", "bar") looks like it's calling a function.
3. o.foo = "bar" doesn't look like it's calling a function.
4. code should look like what it is.
I'm ok with mild to moderate use of getter/setters. But proxies go
too far, imo, and this kind of thing is what has ruined ruby. That's
my esthetic preference, YMMV.
If you DO implement such a thing, I'd recommend sticking to the
Harmony Proxies API strawman. Some thought and planning has gone into
it, and it's somewhat well known.
--i
On Fri, Dec 17, 2010 at 12:40, Isaac Schlueter <i...@izs.me> wrote:
> portable to any other platform. But, your ORMs and such can be really
> cool.
That should read:
"But, your ORMs and such can be really cool-looking douchebags."
My bad. Premature send and all that.
--i
var obj = MGR.manageObject(myUnmanagedClassInstanceOrObjectLiteral, { getter: ... setter: ... });
The managedObject would seal (Object.seal) the object handed to it, preventing arbitrary extensions and then shim in the getters and setters as appropriate. You either need to plan your managed properties carefully, or devise a strategy to clone the original unmanaged object and copy the values of the managed object into it, plus the properties you needed to add.
Proxies seem like an interesting idea, but I can't even begin to think through the expressive implications of having them in the language, though. I think I'd rather build something like that on top of the language (through explicit getters/setters as you describe) than have it shoved into the language underneath me. It seems like debugging a rogue proxy would be challenging.
Nick
____________________
Nicholas Husher
nhu...@gmail.com
http://nickol.us
This is exactly the kind of behavior I was eluding to in my first post.
You could also do this at some stage in a constructor, pulling all
instantiated properties into the managed state.
~ Anders
–Jacob
–Jacob