admin管理员组文章数量:1390564
sometime i need to replace a variable value to another
so i use this method
var $$test = "First",
$$test = "Second";
the code work fine but i use jsfiddle JSHint button to check any error on JavaScript (it helped me a lot)
but i got this error '$$test' is already defined
so what is the ideal method to re define any variable
Thank you :)
sometime i need to replace a variable value to another
so i use this method
var $$test = "First",
$$test = "Second";
the code work fine but i use jsfiddle JSHint button to check any error on JavaScript (it helped me a lot)
but i got this error '$$test' is already defined
so what is the ideal method to re define any variable
Thank you :)
Share Improve this question asked May 19, 2013 at 4:54 Déjà BondDéjà Bond 3014 gold badges9 silver badges32 bronze badges4 Answers
Reset to default 6You're getting that error because you're declaring the same variable twice.
var a = foo, a = bar;
Is the same as:
var a = foo;
var a = bar;
Just break your code in two lines, and you won't get that warning. Like this:
var a = foo;
a = bar;
Also notice that if you declare a variable with a value, and then right after that you change its value, the first line is a noop.
Don't use a ma. You should redefine it as a new statement:
var $$test = 'First';
$$test = 'Second';
this code is trying to define two variable s called $$test. They need to have uniuque names. Try using $$test1 and $$test2
updated for you http://jsfiddle/9CdJN/2/
(function($){
var $$test = "First";
$$test = "Second";
console.log($$test);
})(jQuery);
The problem is that you have a ma ,
at the end of the first line instead of a semi-colon ;
.
Each Javascript statement ends with a semi-colon. You can define multiple variables with a single var
by separating them all with a ma.
var var1=1, var2=2, var3=3;
is the same as
var var1=1,
var2=2,
var3=3;
Because you had a ma in the first line, the browser believes you are declaring two different variables with the same name. To fix it, just change it to this:
var $$test = "First";
$$test = "Second";
本文标签: javascriptvariable is already definedStack Overflow
版权声明:本文标题:javascript - variable is already defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744681769a2619451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论