admin管理员组文章数量:1025278
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
3 Answers
Reset to default 5You 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());
}
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
3 Answers
Reset to default 5You 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());
}
本文标签: scopeJavascriptIncrementing static function variable simulation with closuresStack Overflow
版权声明:本文标题:scope - Javascript - Incrementing static function variable simulation with closures? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614808a2159224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论