[PROPOSAL] "use" keyword

Tab Atkins Jr. jackalmage at gmail.com
Fri Jul 25 12:44:01 PDT 2014


On Fri, Jul 25, 2014 at 11:52 AM, Michaël Rouges
<michael.rouges at gmail.com> wrote:
> Hi all,
>
> There is any plan around un functionnality like the "use" keyword, in PHP.

PHP's use() syntax is because PHP doesn't have actual lexical
closures; it's a hack around the lack.

> Why something like that? Because, in JS, there is no way to inject some
> variables,
> without touch the this object known in a function.

"Injecting variables into a function" is typically done by passing arguments.

> The lone possible way is with ownmade functions, in the same context, by :
>
> `var test,
>     fn;
>
> fn = new Function('value', 'return function (){console.log(value);}');
>
> fn('hello');`

This is functionally identical to:

fn = function(value) { return function() { console.log(value); };
fn("hello")

Using the Function constructor here doesn't seem to offer anything at
all over just writing a normal function.  Am I missing something?

~TJ


More information about the es-discuss mailing list