tags:

views:

122

answers:

4

Title says it all. Why does

if (prev = this.Prev()) {
    ...
}

work but

if (var prev = this.Prev()) {
    ...
}

does not? this.Prev() is a method for a Point object which returns a reference to a previous Point if it exists, and false if it does not. I don't want to declare the variable to be global, and I don't want something verbose like:

var prev = this.Prev();
if (prev) {
    ...
}

EDIT: What's the most elegant way to do something like what I am trying?

+9  A: 

This happens because the if statement expects a expression:

Syntax:

IfStatement :
    if ( Expression ) Statement else Statement 

var is a statement that's why you get a SyntaxError.

Your first example works because an assignment is a expression (AssignmentExpression)

Edit: Let me quote this part:

I don't want to declare the variable to be global

I understand your concern, an assignment made to an undeclared identifier may end up creating a property on the global object, moreover with the ECMAScript 5th Strict Mode, an undeclared assignment will cause a ReferenceError, breaking your code

Variables in JavaScript are declared before the actual code execution, all occurrences of the var statement are bound to the current Variable Object, and they are initialized with undefined, you can't really declare a variable conditionally.

CMS
Okay, thanks. What would you say the cleanest way to handle this is? Something like this.IfPrev(function(prev) {...}) ? (I think you've answered all of my javascript questions in the past few weeks haha).
Nick
I believe I'm understanding that.. I was thinking that if (var prev = this.Prev()), failed, then you would still have a variable called 'prev' that held the value 'false' in the current function scope. But since this syntax is invalid, the point is moot.
Nick
@Nick, IMO your last example is not bad, the callback approach you comment I think is a little bit an overkill for a simple thing. I would recommend you to keep it simple ;)
CMS
+1  A: 

C'est la vie. JavaScript does not let you declare variables there.

(It is a boolean expression and not a statement)

Hogan
A: 

This is because you can't have full keyword-prefixed statements used as expressions. Some statements, like those that aren't prefixed by keywords, can be readily used as expressions without fault.

Delan Azabani
+1  A: 

You say "I don't want to declare the variable to be global". However, keep in mind that JavaScript does not have block scope, but only function scope. Therefore that would not have allowed you to change the scope of the variable, even if it was possible (which is not, as @CMS pointed out).

Quoting Douglas Crockford:

JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages. Define all variables at the top of the function.

Daniel Vassallo
Oh no, I was meaning in my first example that by omitting 'var' it would create the variable as a property of the window, which is not the intended result.
Nick
@Nick: Yes, you're right... But what's wrong with the last example? Why is that too verbose?
Daniel Vassallo
Eh, I guess it's not too bad. I suppose I'm just used to the syntax for other programming languages, and it looked unsightly at first.
Nick