开发者

var x, y = 'foo'; Can this be accomplished?

开发者 https://www.devze.com 2023-03-29 04:46 出处:网络
Since it is possible to do: var x = \'foo\', y = \'foo\'; Would this also be possible? var x, y = \'foo\';

Since it is possible to do:

var x = 'foo', y = 'foo';

Would this also be possible?

var x, y = 'foo';

I tried it, however x b开发者_Python百科ecomes undefined.

I know this may seem like a silly or redundant question, but if I'm curious about something, why not ask? Also you will probably wonder why I would need two variables equal to the same thing in scope. That is not the point of the question. I'm just curious.


Not sure if this is what you're asking, but if you mean "Can I assign two variables to the same literal in one line without typing the literal twice?" then the answer is yes:

var x = 10, y = x;


You need two separate statements to get the exact same semantics.

var x, y; x = y = 'foo';
// or
var x = 'foo'; var y = x;
// or
var y; var x = y = 'foo';

The solution other users are suggesting is not equivalent as it does not apply the var to y. If there is a global variable y then it will be overwritten.

// Overwrites global y
var x = y = 'foo';

It is a good practice to correctly apply var to local variables and not omit it for the sake of brevity.


You can do

var x = y = 'test'; // Edit: No, don't do this

EDIT

I just realized that this creates/overwrites y as a global variable, since y isn't immediately preceded by the var keyword. So basically, if it's in a function, you'd be saying "local variable x equals global variable y equals …". So you'll either pollute the global scope, or assign a new value to an existing global y variable. Not good.

Unfortunately, you can't do

var x = var y = 'test'; // Syntax error

So, instead, if you don't want to pollute the global scope (and you don't!), you can do

var x, y;
x = y = 'test';


In order for that to work, you will either need to initialize them separately (like your first example) or you will need to set them in a separate statement.

// This causes bugs:
var x = y = 'test';

Watch:

var y = 3;
function doSomething(){ var x = y = 'test'; }
doSomething();
console.log( y ); // test !?

On the other hand:

// this does not
var x,y; x = y = 'test';

Watch:

var y = 3;
function doSomething(){ var x,y; x = y = 'test'; }
doSomething();
console.log( y ); // 3 -- all is right with the world.


Below is my test function. The currently uncommented line in the pollute function does what you were looking for. You can try it and the other options in jsfiddle here.

var originalXValue = 'ekis';
var originalYValue = 'igriega';
var x = 'ekis';
var y = 'igriega';

function pollute()
{
    // Uncomment one of the following lines to see any pollution.
    // x = 'ex'; y = 'why';      // both polluted
    // var x = 'ex'; y = 'why';  // y was polluted
    // var x = y = 'shared-ex';  // y was polluted  
    var x = 'oneline', y = x;      // No pollution
    // var x = 'ex', y = 'ex';   // No pollution

    document.write('Pollution function running with variables<br/>' +
                   'x: ' + x + '<br/>y: ' + y + '<br/><br/>');
}

pollute();

if (x !== originalXValue && y !== originalYValue)
{
    document.write('both polluted');
}
else if (x !== originalXValue)
{
    document.write('x was polluted');
}
else if (y !== originalYValue)
{
    document.write('y was polluted');
}
else
{
    document.write('No pollution');
}


Note that although

var x = y = 'test';

is legal javascript

In a strict context (such as this example):

function asdf() {
    'use strict';
    var x = y = 5;

    return x * y;
}
asdf();

You will get:

ReferenceError: assignment to undeclared variable y

to have it work without error you'd need

var x, y;
x = y = 5;


You'd use var x, y = 'foo' when you want to explicitly initialize x to undefined and want to restrict the scope of x.

function foo() {
  var x, y = 'value';
  // ...
  x = 5;
  // ...
}
// Neither x nor y is visible here.

On the other hand, if you said:

function foo() {
  var y = 'value';
  // ...
  x = 5;
  // ...
}
// y is not visible here, but x is.

Hope this helps.

Source: http://www.mredkj.com/tutorials/reference_js_intro_ex.html


I would avoid being tricky. Since I only use one variable per var (and one statement per line) it's really easy to keep it simple:

var x = "hello"
var y = x

Nice, simple and no silly issues -- as discussed in the other answers and comments.

Happy coding.


I am wondering why nobody posted that yet, but you can do this

var x, y = (x = 'foo');


You can't do

var a = b = "abc";

because in that case, b will become a global variable.

You must be aware that declaring a variable without var makes it global. So, its good if you follow one by one

 var a = "abc";
 var b = a;
0

精彩评论

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