jQuery更多变量分号或逗号

编程入门 行业动态 更新时间:2024-10-22 13:53:50
jQuery更多变量分号或逗号 - 基本(jQuery more variables semicolon or comma - basic)

我知道我们可以使用一个var用于许多变量并使用逗号分隔它们

var text1 = 'abc', text2 = 'bcd', text3 = 'dfg'

但如果我的变量是

var text1 = 'abc', text2 = 'bcd', text3 = 'dfg', text4 = $(this).find('div').next('li'); text5 = $(this).find('div2').next('li2');

我发现text4末尾的逗号不正确,如果像这样的情况我们还需要逗号分隔或分号与前面的var ?

I know we can just use one var for many variables and use comma to separate them

var text1 = 'abc', text2 = 'bcd', text3 = 'dfg'

but if my variables are

var text1 = 'abc', text2 = 'bcd', text3 = 'dfg', text4 = $(this).find('div').next('li'); text5 = $(this).find('div2').next('li2');

I found comma at the end of text4 is not correct, if the case like that do we still need comma to separate or semicolon with a var in the front?

最满意答案

var var1 = val1, var2 = val2;

相当于:

var var1 = val1; var var2 = val2;

逗号用于在一个var语句中分隔多个变量声明,分号用于分隔多个语句。 所以当你使用分号时,你必须再次使用var关键字,因为你正在开始一个新的语句。

如果使用分号并且不在新语句中使用var关键字,则将分配全局变量,而不是声明本地变量。

var var1 = val1, var2 = val2;

is equivalent to:

var var1 = val1; var var2 = val2;

Commas are for separating multiple variable declarations within one var statement, semicolons are for separating multiple statements. So when you use semicolon, you have to use the var keyword again because you're starting a new statement.

If you use a semicolon and don't use the var keyword in the new statement, you'll be assigning a global variable, not declaring a local one.

更多推荐

本文发布于:2023-08-07 17:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分号   逗号   变量   更多   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!