lundi 26 janvier 2015

JavaScript String.split on a string literal to produce an array


I've seen a few javascript programmers use this pattern to produce an array:



"test,one,two,three".split(); // => ["test", "one", "two", "three"]


They're not splitting user input or some variable holding a string value, they're splitting a hard-coded string literal to produce an array. In all of the cases I've seen a line like the above it would seem that it's perfectly reasonable to just use an array literal without relying on split to create an array from a string. Are there any reasons that the above pattern for creating an array makes sense, or is somehow more efficient than simply using an array literal?





Aucun commentaire:

Enregistrer un commentaire