Is there Object.watch for all properties / a shim for __noSuchMethod__ available?
Asked Answered
P

2

1

I would like to extend localStorage by executing some code each time a setting is fetched/stored. I could define a getter/setter for all existing properties of localStorage, but the problem lies in new settings.

For example, localStorage['somenewsetting'] = 123 creates a new setting. I would like to automatically define a getter/setter for this property so that my addition code is also executed for new settings.

Basically I need Object.watch, but for all properties. What I found was __noSuchMethod__, but it is not available on Chrome. Is there any shim available for this method? Or is there another way of watching all properties of an object?

Again, I need to make this work on Chrome.

Paulettapaulette answered 21/6, 2011 at 17:35 Comment(1)
How about accessing all assignments using setItem. I wrote a wrapper/façade class for localStorage/sessionStorage to serialize RegExps and even strings (as quoted strings) and to deserialize plain objects, Dates, and RegExps. You could do the same with the intent of adding your additional code inside the setItem proxy function.Separation
L
2

Nope, at least, not for Chrome. __noSuchMethod__ only works for functions, anyway.

Proxy support is under discussion for the next version of ECMAScript (Harmony), and even already implemented in SpiderMonkey. Until then, you're out of luck, I'm afraid.

— there was a request to implement __noSuchMethod__ in V8 but it was refused. Requests to implement Proxy haven't been any more successful: 633 was merged as duplicate, and the Chromium team doesn't care much about implementing Proxy support.

Ludmilla answered 21/6, 2011 at 17:50 Comment(0)
D
2

Yeah, I want that too. So do many, many other people. Isn't going to happen. Ever. Chrome (V8) follows webkit in terms of its JS API. __noSuchMethod__ is a non-standard event created by Mozilla and has been officially rejected from ECMAScript (JavaScript).

In other words, it will never exist :'(

(or if it does, it will be after much arguing and it won't be any time soon)

Dissipated answered 21/6, 2011 at 17:47 Comment(1)
Nope, you can't work around it as it's far too low-level. Well you can patch chrome (there's a patch if you look for it), but that's only really helpful for you, not development in general :)Dissipated
L
2

Nope, at least, not for Chrome. __noSuchMethod__ only works for functions, anyway.

Proxy support is under discussion for the next version of ECMAScript (Harmony), and even already implemented in SpiderMonkey. Until then, you're out of luck, I'm afraid.

— there was a request to implement __noSuchMethod__ in V8 but it was refused. Requests to implement Proxy haven't been any more successful: 633 was merged as duplicate, and the Chromium team doesn't care much about implementing Proxy support.

Ludmilla answered 21/6, 2011 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.