Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #41044
| Path | csiph.com!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.205.59!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: [Proxy] No way to hook property creation? |
| Date | Mon, 22 Aug 2022 01:08:06 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 74 |
| Message-ID | <2658833.mvXUDI8C0e@PointedEars.de> (permalink) |
| References | <nfjfbn7so9tq$.10vkffqwvv24i.dlg@40tude.net> |
| Reply-To | Thomas 'PointedEars' Lahn <usenet@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| Injection-Info | gwaiyur.mb-net.net; posting-host="178.197.205.59"; logging-data="1505928"; mail-complaints-to="abuse@open-news-network.org" |
| User-Agent | KNode/4.14.10 |
| Cancel-Lock | sha1:AF9rpRAgqZU1f8+WmLipifDI0l4= |
| X-User-ID | U2FsdGVkX1/SB3YLxXfCQz01pG9OrD3LJdqKmh6QlMmiCmRJkBD/Xg== |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| Xref | csiph.com comp.lang.javascript:41044 |
Show key headers only | View raw
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