JS Lint Array Line Separator Alphabet

I know that JSLint is just a guide, and you should take what it says with salt, but I'm curious how I can even resolve this warning without rewriting the entire function. Here is the function of interest:

function randomString(length) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split(''), str = '', i; if (!length) { length = randomNumber(chars.length); } for (i = 0; i < length; i++) { str += chars[randomNumber(chars.length)]; } return str; } 

JS Lint tells me: "JS Lint: use literary array notation []". and it points to a line with string.split() . How can I satisfy JSLint without rewriting the entire function? Is it possible?

I know that there are other methods for generating random strings; I am interested in how to enable JSLint warning using this method.

+6
source share
2 answers

You can call the String prototype split function with the string as the scope to avoid the warning:

 var chars = String.prototype.split.call('ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz', ''), 

I don't know why JSLint complains, since split is a String method.

See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

this also seems to go without complaint:

 var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz', chars = alphabet.split(""); 
+3
source

Here is your array in an array literal entry:

 [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'T', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ] 

JSLint probably suggested that the interpreter does not need to split the String at run time, but it is ready to use.

Just generated using this PHP code:

 php > $chars = str_split('ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'); php > echo "[ '".implode("', '", $chars)."' ]"; [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'T', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ] php > 
+4
source

Source: https://habr.com/ru/post/908347/


All Articles