How to modify the scope chain without `with` ?

Coroutines coroutines at gmail.com
Mon Feb 22 09:34:48 UTC 2016


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.


More information about the es-discuss mailing list