最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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());
}
发布评论

评论列表(0)

  1. 暂无评论