最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

scope - Javascript - Incrementing static function variable simulation with closures? - Stack Overflow

matteradmin7PV0评论

So it seems like there are no function static variables in Javascript. I am trying to increment a variable inside a function, but I do not want to do it like this:

function countMyself() {
    if ( typeof countMyself.counter == 'undefined' ) {
        // It has not... perform the initilization
        countMyself.counter = 0;
    }
}

I would like to do it with the closures, but I am having a really hard time to understand these.

Someone suggested this in another question:

var uniqueID = (function() {
   var id = 0;
   return function() { return id++; };
})();

But all it does when I alert uniqueID, is printing this line: return function() { return id++; };

So I would like to know how to increment a variable in a function without polluting the global scope.

So it seems like there are no function static variables in Javascript. I am trying to increment a variable inside a function, but I do not want to do it like this:

function countMyself() {
    if ( typeof countMyself.counter == 'undefined' ) {
        // It has not... perform the initilization
        countMyself.counter = 0;
    }
}

I would like to do it with the closures, but I am having a really hard time to understand these.

Someone suggested this in another question:

var uniqueID = (function() {
   var id = 0;
   return function() { return id++; };
})();

But all it does when I alert uniqueID, is printing this line: return function() { return id++; };

So I would like to know how to increment a variable in a function without polluting the global scope.

Share Improve this question asked Oct 5, 2013 at 2:30 user1834464user1834464 3
  • BTW, what's wrong with the first implementation? – jfriend00 Commented Oct 5, 2013 at 2:42
  • What you are after is what's known as "private members". The definitive article on the subject (in my opinion at least) is Douglas Crockford's Private Members in JavaScript. – Beetroot-Beetroot Commented Oct 5, 2013 at 2:42
  • The conditional in the first version could be a one-liner: countMyself.counter = countMyself.counter || 0; – bfavaretto Commented Oct 5, 2013 at 3:25
Add a ment  | 

3 Answers 3

Reset to default 5

You must actually call uniqueID - you can't just refer to is as if it were a variable:

> uniqueID
function () { return id++; }

> uniqueID()
0

> uniqueID()
1

Assigning to uniqueID explicitly instead of returning it from the immediately-invoked lambda might make things clearer:

var uniqueId; //declare uniqueId in the outer scope

function initializeUniqueId(){
    var id=0;
    uniqueId = function(){
        return id++;
    }
}

initializeUniqueId();

console.log( uniqueId ); //what you are currently doing
console.log( uniqueId() }; //what you should be doing.

The advantages of the version with the (function(){}()) pared to the one I just wrote are that the initializer function is anonymous and that you only need to write "uniqueId" once.

Simple see this code I want to increment the '_a' variable

in ES6

let a = () => {
  let _a = 1;
  return () => _a++;
}

let clo = a();
for (var i = 0; i < 5; i++) {
  console.log(clo());
}

in ES5

var a = function() {
  var _a = 1;
  return function() {
    return _a++;
  }
}

var clo = a();
for (var i = 0; i < 5; i++) {
  console.log(clo());
}
Post a comment

comment list (0)

  1. No comments so far