最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - variable is already defined - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 6

You'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";
发布评论

评论列表(0)

  1. 暂无评论