C-style static variables

In C we have static variable that can be used within functions.
Example in C

int increment() {
    static int num = 0;
    num++;
    return num;
}
increment(); //1
increment(); //2
increment(); //3

It would have been nice we could have this same functionality in JavaScript.

function increment() {
    static num = 0;
    num++;
    return num;
}
increment() //1
increment() //2
increment() //3

This helps us to encapsulate logic within our function without polluting the global scope.
static is already a keyword so this would be easy to implement.
What do you guys think about this?
Let me know!

We basically already have that with block statements:

{
  let a = 1;
  function b() { return a++; }
}

console.log(b(), b(), b()); // 1, 2, 3

And I'm not quite sure this is a good idea for a couple of reasons:

(1) How do you declare variables, with var, let, const or static? Adding yet another keyword makes it harder to learn the language, and even harder for people coming from other languages that occasionally work with JS.

(2) JavaScript has a lot of concepts that are half-way broken, scoping is already very complicated (var having more or less function scoping, let and const having block scoping, the Web Compat semantics), so adding yet another variant, variables that are shared among Environment Records of the same Function (or do they get shared between all Environment Records of all Functions derived from one function expression?, what about new Function("static num = 0; return num++") ?)

(3) JavaScript already has closures, which are a very powerful concept, which also works for your usecase as shown above, and is quite easy to understand. Do you have other usecases in mind?

2 Likes

Thanks for the wise reply. This concept of static variables works well with C and functional programming. I just came up with this idea as a syntactic sugar. This concept is already implemented with classes (static methods). So implementation is quite easy. This is a tried and tested features in a mature language like C. Implementation of new features adds a bit more learning curve to every language so this feature alone will not trip up beginners.
var will likely get depricated.
In C this is actually used as:

static int num = 0;

so in JS, my initial syntax could be written as:

static let num = 0;

(I didn't suggest it initially because: static const num = 0 doesn't make any sense. And it looks plain ugly)
The major issues I figured out from your comment is about scoping.Scoping is problematic.
static variables could help us enforce more rigid encapsulation.
This doesn't work outside a function so its not exactly “just another way to declare a variable”
I appreciate closures. Going with closures seems like a better idea. Thank!

While the same effect can be achieved with closures, I actually strongly support this! It's an inconvenience to have to declare objects outside all the time to avoid unnecessary allocations. I have made a new topic for a syntax inside expressions, and have included this as an extension. Similarly to classes, I've made static variables accessible outside as a simple accessor on the function. Let me know what you think. (my proposal)

Ok you do it your way and that's what I still do .great

Tải Outlook for iOS