[proposal] Persistent variables in functions/methods

Neek Sandhu neek.sandhu at outlook.com
Tue Jul 17 05:51:53 UTC 2018


> re at Ben


  1.  `persist` because didn’t want to bring in the dark connotation associated with `static` variables in C++. Although `persist` is just an example to prove a point, could be anything really (even `static` 😊)
  2.  As for my knowledge and understanding, I’d imagine these to behave just like closures w/ IIFE

```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;
    }
})()
```

Revisions to this are welcome of course



  1.  Ref #2
  2.  Umm…. Lets just say the fact we need to preserve state (like `counter` or `lastValue`) is good enough selling point
  3.  This proposal aims for and enforces, once again, the motto that stuff should “live” where it “belongs” and not “leak” out unnecessarily
  4.  Ref #5 and also see my reply to jhpratt that explains how this fails when class methods are in question (see `DBService` class example)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180717/0a4b1d4a/attachment-0001.html>


More information about the es-discuss mailing list