[proposal] Persistent variables in functions/methods

Ben Wiley therealbenwiley at gmail.com
Tue Jul 17 05:26:40 UTC 2018

I generally think JavaScript is a much better looking language than C++ but
this is one of my favorite C++ features, so it's interesting to see it
considered for JavaScript.

A few questions:

1. I'm wondering if there's a strong argument for using the word `persist`
rather than `static` as C++ does. Certainly this isn't the same thing as a
static class member but they're not dissimilar concepts, and use areas are
different enough to avoid language ambiguity.
2. Does the static initialization have access to non-static variables,
function parameters or `this` members? Might be less error prone if not,
but maybe they are compelling use cases for that.
3. Many folks in C++ land seem to think static variables are overused and
abused. Do we think the benefits outweigh the pitfalls here?
4. Are we certain JS engines can't/don't perform the optimization that we
would get from static const variables?
5. Doesn't module level scope in es2015+ give us basically the same benefit
we would get for static const vars in functions? Sure it's not declared in
the same place, but it's still well encapsulated and not available off the
object or class itself.
6. Is there a great reason for static let rather than defining the "static"
variable in an outer closure per your code example?

My own tentative answers are:
1. static is better than persist unless we really think people won't
understand how it's different than static class members
2. disallow unless there's a clearly good use case
3. not sure
4. no idea
5. I think I always use module scope in JS where I would use static const
in C++. Seems pretty good even if it reads a bit differently. Arguably
better because variable lifetime is apparent based on indentation.
6. I think there's an argument here for conciseness.. other than that I'm
not sure what the reason would be (unless you have a good use case for #2).


Le mar. 17 juill. 2018 00 h 56, Neek Sandhu <neek.sandhu at outlook.com> a
écrit :

> It'd be really useful to have variables inside methods/functions that are
> initialized once, reused for subsequent calls and live as long as
> containing scope i.e the function itself.
> > not to be confused with `static` properties
> ## Use Cases
> Almost every app has a function or method that needs to "preserve" some
> state across multiple calls, a counter for example. Current ways out of
> this situation are either closures created with IIFE's or making those
> variables top-level. Both of which are ugly. I think the same could be done
> much more neatly with persistent variables.
> ## Example
> ```javascript
> function foo(n) {
>     // initialized on first call to foo, persists for subsequent calls
>     persist let counter =  0;
>     // this way JS engine won't have to recompile the pattern everytime
>     persist const httpRE = /^https?/;
>     counter++;
>     return n * a;
> }
> ```
> is 1:1 as
> ```javascript
> let foo = (() => {
>     let counter = 0;
>     const httpRE = /^https?/;
>     return (n) => {
>         counter++;
>         return  n * a;
>     }
> })()
> ```
> _______________________________________________
> 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/20180717/4c78f300/attachment.html>

More information about the es-discuss mailing list