How to modify the scope chain without `with` ?
Isiah Meadows
isiahmeadows at gmail.com
Mon Feb 22 12:07:45 UTC 2016
FYI, this part might not work as you might expect. I've already been
tripped up by similar.
```js
var o = {
index: 0,
func() { return this.index++ },
}
with (o) {
func() // TypeError: Cannot read property 'index' of undefined
}
```
The other reasons `with` is deprecated are because dynamic scoping is
extremely hard to optimize and it's no longer obvious what variables
are defined in that block (static analysis no longer works).
As for the global scope, I'll refer you to the System.global proposal:
https://github.com/tc39/proposal-global
Isiah Meadows
me at isiahmeadows.com
On Mon, Feb 22, 2016 at 4:34 AM, Coroutines <coroutines at gmail.com> wrote:
> To me "don't use `with` - assign the object to a smaller reference"
> just seems unsustainable.
>
> Say you're using a bunch of functions from 'ReallyLongModuleName'.
>
> So you do something like this:
>
> var x = require('ReallyLongModuleName');
>
> And you start doing this everywhere:
>
> x.something();
> x.thatThingOverThere();
> console.log(x.Whatever() + x.thisIsAnnoying());
>
> It just becomes a chore to write "x." everywhere - which is what makes
> `with (x) { ... }` so attractive.
>
> Again, this is predicated on you wanting to use say 15+ functions from
> a module that has a really long name. And that's assuming it's the
> only module with a really long name. Some people (me) have trouble
> coming up with short, meaningful names for things which ordinarily
> would have long names. So these people (me) would use common short
> identifiers - like: var $ = require(...);
>
> Well shit, now we're confusing people further. var x, y, z, $ =
> require('this has to end somewhere');
>
> Global scope should be something you can replace and prototype. Maybe
> not for security - not a sandbox - but for managing how concisely one
> can refer to the functions they need to. (Again: For instances where
> you're using many functions from a module and it makes sense to just
> populate the object acting as the global scope with them).
>
> We should be able to manage `global`/`window` better, and I think this
> has merit for ES7.
>
> PS: `with` only allows you to put an object at the front of the scope
> chain, what I'm suggesting would allow you to fallback on another
> scope with inheritance.
>
> PPS: Some modules overwrite the identifiers they use in the global
> scope, even when they assign into module.exports (which is naughty).
> Being able to portably construct the global scope object a function
> runs in can prevent this messiness. We need Node's
> https://nodejs.org/api/vm.html#vm_vm_runincontext_code_contextifiedsandbox_options
>
> Personally I'd prefer a special keyword:
>
> _ENV = {}; // empty global scope, no Array, no String, but still
> reachable through [].prototype of course.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
More information about the es-discuss
mailing list