Indirect references to variables

John Gardner gardnerjohng at gmail.com
Wed Dec 9 05:56:05 UTC 2015


Trouble is, if one has literally hundreds of classes or functions that need
to be matched, they'd rather not pool them all into one massive object
literal for the sake of easier mapping. DRY principle fully relevant.

Also, yes, while it would be a pain for static type analysis, it wouldn't
necessarily be the same as `eval`. Eval executes arbitrary code, whereas
the indirect references would only point to modifiers only:

    var className = \"doSomethingSinister(/etc/passwd/);"

That line would literally be looking for a property in the current scope
that'd be named this:

    global["doSomethingSinister(/etc/passwd/);"]
    window["doSomethingSinister(/etc/passwd/);"]



On 9 December 2015 at 16:09, Frankie Bagnardi <f.bagnardi at gmail.com> wrote:

> This is a common situation, but one easily solved by object literals.
> Reflecting on the scope is confusing and would hurt tooling (it's
> essentially eval).
>
> ```js
> var mapping = {Polygon: Polygon};
> var meshClass = mapping[ajaxData.className];
> ```
>
>
>
>
> On Tue, Dec 8, 2015 at 9:53 PM, John Gardner <gardnerjohng at gmail.com>
> wrote:
>
>> ECMAScript currently offers no clean way to "dereference" a variable in
>> the current scope. For instance, assume an author wishes to obtain a
>> reference to a class using a variable that holds its name:
>>
>>     class Paintbrush{  }
>>
>>     let className = "Paintbrush";
>>
>>     // Would only work in browsers, not NodeJS
>>     console.log( window[className] );
>>
>>     // Doesn't even work in NodeJS
>>     console.log( global[className] || this[className] );
>>
>> A hacky workaround is to create an anonymous function that simply returns
>> a reference to the named variable:
>>
>>     function dereference(name){
>>         return new Function([], "return " + name)();
>>     }
>>     dereference("Paintbrush") === Paintbrush; // true
>>
>> This isn't an elegant solution, nor a preferable one. Another approach
>> might be to leverage `eval`, which opens up the obvious issues of
>> performance and security.
>>
>> Having a way of indirectly referencing another variable would fix this:
>>
>>     class Paintbrush{  }
>>
>>     let className       = "Paintbrush";
>>
>>     let classReference  = \className;
>>     console.log(classReference === Paintbrush); // true
>>
>> Sticking a backslash before a bareword identifier creates a reference to
>> an object whose name matches the identifier's string value. If no such
>> object exists in the current scope, it simply returns `undefined`.
>>
>> I can't see this being used in everyday programs, but it would facilitate
>> Ajax programming considerably, where classes or functions can only be
>> specified by name:
>>
>>     {"className":"Polygon", "vertices": [[0,0]...] }
>>
>>     let meshClass = \ajaxData.className;
>>     if(meshClass instanceof Mesh){
>>         new meshClass(ajaxData.vertices);
>>     }
>>
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151209/713ea1fc/attachment-0001.html>


More information about the es-discuss mailing list