On Tue, Jul 17, 2018 at 6:06 AM, Jacob Pratt <jhpratt...@gmail.com> wrote: > Would this not work? > > ```javascript > function foo(n) { > this.counter++; > return n * a; > } > foo.counter = 0; > foo.httpRE = /^https?/; > ```
Did you mean `foo.counter++;`? Because to make `this` inside a call to `foo` be `foo` itself, you'd have to call `foo` like this: `foo.call(foo, 42)`. `counter` and `httpRE` are also fully exposed. Not desirable IMHO. On Tue, Jul 17, 2018 at 6:51 AM, Neek Sandhu <neek.san...@outlook.com> wrote: > is 1:1 as > > ```javascript > let foo = (() => { > let counter = 0; > const httpRE = /^https?/; > return (n) => { > counter++; > return n * a; > } > })() That isn't 1:1 with what I understand you're propoing. `counter` and `httpRE` are initialized when `foo` is created, not when it's first called. (IIRC, this is true of C's `static` variables, too, but it's been ~25 years...) Another existing pattern to compare with (which also initializes them up-front, not on first call): ```js let foo; { let counter = 0; const httpRE = /^https?/; foo = (n) => { counter++; return n * a; }; } ``` You can defer init by rewriting `foo`, but I **really** don't like that idea, not least because anything grabbing `foo` before calling it keeps the wrong one): ```js function foo(n) { let counter = 0; const httpRE = /^https?/; foo = (n) => { counter++; return n * a; }; return foo(n); } ``` Again, I really don't like that, but it does defer init. Given how easy this is with block scope or, as Ben Wiley points out, module scope, I think there would need to be a stronger use case. -- T.J. Crowder
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss