开发者

Defining numerous variables in a single step?

开发者 https://www.devze.com 2023-01-08 02:13 出处:网络
Is there a solution I can use that allows me to define more than one var with the same value in a single step at the start of my func开发者_如何学运维ion?

Is there a solution I can use that allows me to define more than one var with the same value in a single step at the start of my func开发者_如何学运维ion?

function myFunction () {
    var a,b = 0;
    document.write(a) // undefined
    document.write(b) // 0
}

Is there an improved way to write a,b = 0; ?


Something like this, however I don't like it.

var var1 = "hello",
    var2 = "world",
    var3 = 666;

Better

var var1 = "hello";
var var2 = "world";
var var3 = 666;

Please take a look at http://javascript.crockford.com/code.html


You can't do two things at once. You can't declare multiple local variables and assign a single value to all of them at the same time. You can do either of the following

var a = 1, 
    b = 1;

or

var a,b;
a = b = 1;

What you don't want to do is

var a = b = 1;

because you'll end up with b being a global, and that's no good.


var a = 0, b = 0;


var a = 0, b = a;


An alternate way

var a = b = 0; 
0

精彩评论

暂无评论...
验证码 换一张
取 消