I found the following js sample and am confused by the syntax. Notice the statements are separated by commas instead of semicolons. Are commas a valid statement separator in js? I have not seen this before.
$('selector').each(function () {
this.onclick = function () {
this.select();
},
this.onblur = function () {
},
this.onfocus = function () {
},
this.onkeyup = function () {
}
});
Commas act as a separator between expressions in a single expression statement. Thus, that (if it had been completed instead of being cut off after the “onkeyup” function) is just a single expression statement.
There’s really no reason to code like that, or no really good reason at least. In this particular case it has essentially the same effect as would a series of separate expression statements separated by semicolons.
The comma “operator” is questionable in many cases but useful sometimes:
for example. It allows one to drop more than one expression (assignments usually) into a grammatical locale that allows just one expression. It’s really a sign of syntactic weakness, in my opinion.