我知道我们可以使用一个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.
更多推荐
发布评论