Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #41044
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: [Proxy] No way to hook property creation? |
| Date | 2022-08-22 01:08 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2658833.mvXUDI8C0e@PointedEars.de> (permalink) |
| References | <nfjfbn7so9tq$.10vkffqwvv24i.dlg@40tude.net> |
JJ wrote:
^^
Please post here using your real name.
> I'm trying to use Proxy and the `with` statement to redirect access to the
> global object.
>
> The problem is that, Proxy doesn't seem to have any handler for property
> creation which is done without object accessor or
> `Object.defineProperty()`. e.g. `prop = 123` instead of `obj.prop = 123`
The (by-design) inconsistent and unpredictable behavior of the *legacy*
“with” statement that *never* a property on the target object is being
created with implicit access, but either
a) the property of an object higher up in the scope chain is modified –
or not if it is read-only or has a dismissive object value getter
(host object) or property setter,
or
b) a property of the global object is created (or not)
is one of the reasons why it is *deprecated* since ECMAScript Edition 5,
and *code containing it cannot even be executed* in Strict Mode.
This has nothing to do with the capabilities of the Proxy object because its
instance does not even "see" that write access.
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with>
You can determine whether a property is being created in the “set” handler
of the Proxy instance:
var target = {};
var proxy = new Proxy(target, {
set (target, propertyName, value) {
var propertyExisted = (propertyName in target);
target[propertyName] = value;
if (!propertyExisted) {
console.log(`Created property "${propertyName}" on target with value
"${value}".`);
}
}
});
/*
* displays
* "Created property "foo" on target with value ""bar""
*/
proxy.foo = "bar";
/* does not display anything */
proxy.foo = "baz";
Tested in Chromium 90 on GNU/Linux,
navigator.userAgent == "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36".
The usual caveats for the “in” statement and ES 6+ features apply.
See also:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy>
--
PointedEars
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2
Please do not cc me. /Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[Proxy] No way to hook property creation? JJ <jj4public@outlook.com> - 2022-08-07 12:56 +0700
Re: [Proxy] No way to hook property creation? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2022-08-20 17:17 -0700
Re: [Proxy] No way to hook property creation? JJ <jj4public@outlook.com> - 2022-08-21 14:30 +0700
Re: [Proxy] No way to hook property creation? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2022-08-21 15:37 -0700
Re: [Proxy] No way to hook property creation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2022-08-22 01:08 +0200
Re: [Proxy] No way to hook property creation? JJ <jj4public@outlook.com> - 2022-08-23 03:31 +0700
Re: [Proxy] No way to hook property creation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2022-08-24 01:45 +0200
Re: [Proxy] No way to hook property creation? TheScreamingFedora <sebaslogo@gmail.com> - 2022-08-25 08:39 -0700
Re: [Proxy] No way to hook property creation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2022-08-27 22:40 +0200
csiph-web