admin管理员组文章数量:1277899
I have multiple booleans defined in Javascript:
On toggle I would like to set these to false or true.
var categoryAdvertising = false;
var categoryInformArtation = false;
var categoryACA = false;
var categoryEntertainment = false;
var categoryInfluencing = false;
var categoryICE = false;
var categoryCommunication = false;
var categoryParticipation = false;
What is the best way to set these variables? Using an array?
Thanx in advance
I have multiple booleans defined in Javascript:
On toggle I would like to set these to false or true.
var categoryAdvertising = false;
var categoryInformArtation = false;
var categoryACA = false;
var categoryEntertainment = false;
var categoryInfluencing = false;
var categoryICE = false;
var categoryCommunication = false;
var categoryParticipation = false;
What is the best way to set these variables? Using an array?
Thanx in advance
Share Improve this question edited Jan 9, 2012 at 11:52 Headshota 21.4k13 gold badges62 silver badges82 bronze badges asked Jan 9, 2012 at 11:49 EmrulezEmrulez 4191 gold badge6 silver badges15 bronze badges 3- If I had to manage these many boolean values, which are apparently related to each other in some manner, I would surely look at Flags/Bitmasks. Fortunately, MDN provides a very good example of using "Flags/Bitmaks": – Golmaal Commented Jan 9, 2012 at 12:23
- @Golmaal funny thing about that article is that every flag is stored in a variable anyway, and javascript numbers take 8 bytes each. – Esailija Commented Jan 9, 2012 at 12:26
- @Esailija, Yes, but saving memory was never the key objective. I think the "flags/bitmasks" scheme makes the whole thing more pact and easy to manage. – Golmaal Commented Jan 9, 2012 at 12:33
3 Answers
Reset to default 7Just use an object:
var category = {
Advertising: false,
InformArtation: false,
ACA: false,
Entertainment: false,
Influencing: false,
ICE: false,
Communication: false,
Participation: false
};
for( var key in category ) {
category[key] = false;
}
To access object keys:
alert( category.Advertising );
Absolutely yes. You should either use an Array
or if you need clear identifiers, use an Object
.
var config = {
categoryAdvertising: false,
categoryInformArtation: false,
categoryACA : false
// etc
};
and then toggle all like
Object.keys( config ).forEach(function( opt ) {
config[ opt ] = true; // or false
});
Disclaimer: This answer contains code that uses features not present in legacy browsers, the following links are suggestions of how to emulate those features in legacy browsers:
Object#keys
Or even better, always use an ES5shim like:
https://github./kriskowal/es5-shim
If you really need to set them all to true
or false
regularly, one wonders if you really need that many of them?
But if you do, use a function. Within the function, you can use a stacked assignment, e.g.:
function setVars(value) {
categoryAdvertising =
categoryInformArtation =
categoryACA =
categoryEntertainment =
categoryInfluencing =
categoryICE =
categoryCommunication =
categoryParticipation =
value;
}
I was going to give you a second option, but Esailija got there ahead of me (+1).
本文标签: javascriptEasily set multiple variables to false or trueStack Overflow
版权声明:本文标题:javascript - Easily set multiple variables to false or true - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741276141a2369737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论