带逗号的 JS 语句分隔符 [英] JS statement separator with commas

查看:33
本文介绍了带逗号的 JS 语句分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了以下 js 示例,但对语法感到困惑.请注意,语句用逗号而不是分号分隔.逗号是 js 中有效的语句分隔符吗?我以前从未见过这个.

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 () {
            }
    });

推荐答案

逗号在单个表达式语句中充当表达式之间的分隔符.因此,那(如果它已经完成而不是在onkeyup"函数之后被切断)只是一个单一的表达式语句.

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:

var index, len;
for (index = 0, len = list.length; index < len; ++index) { ... }

例如.它允许将多个表达式(通常是赋值)放入一个只允许一个表达式的语法语言环境中.在我看来,这确实是句法缺陷的表现.

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.

这篇关于带逗号的 JS 语句分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆