开发者

How to handle magic number in javascript

开发者 https://www.devze.com 2023-03-07 00:35 出处:网络
I have search through google with keyword handle javascript magic number but 开发者_StackOverflow中文版couldn\'t find one.

I have search through google with keyword handle javascript magic number but 开发者_StackOverflow中文版couldn't find one. In c# i would used constant defined number but i don't know what to use in javascript

something like this

public class MagicNumberHelper
{
  const int optionA = 1;
  const int optionB = 2:
  const int optionC = 3
}


Translating that to JS (one of the ways to do it):

var MagicNumberHelper = {
  optionA: 1,
  optionB: 2,
  optionC: 3
}


is it just a constant number that you can test for later in that case

var MagicNumbers = {
 optionA: 1,
 optionB: 2,
 optionC: 3
};

and then later test for it like this

if(inputNr == MagicNumbers.optionA) {
  // your logic here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号