0

Short questioion, I'm trying to understand this tutorial: http://superdit.com/2011/02/09/jquery-memory-game/

Being new to Javascript I can't seem to find what the statement '== ""' means... I understand "==", but not the empty double quotes.

4

3 回答 3

7

val == "" is a non-strict comparison to emtpy string. It will evaluate to true if val is empty, 0, false or [] (empty array):

var val = "";
console.log( val == "" ); // true

val = 0;
console.log( val == "" ); // true

val = false;
console.log( val == "" ); // true

val = [];
console.log( val == "" ); // true

You can use === to use strict comparison, fex:

val = 0;
console.log( val === "" ); // false
于 2012-10-10T07:34:54.400 回答
1

The ' == "" ' is a check for an empty string. It will be true when the string is empty, and false whenever there are some characters inside it.

于 2012-10-10T07:31:33.043 回答
0

A quick scan of the code (ctrl-F is your friend) quickly teaches you that the only time such a statement occurs in the code is here: if (imgopened == ""), another search taught me that imgopened is an evil (global) variable that is initialized to "" at the very top of the script, and every time some action/function is done with whatever value it was assigned.

I suspect it's a sort of card game, where two identical imgs need to be clicked, in which case this var will reference the image currently turned. If it's empty, then all imgs are facing down, and this var is empty: "".
In other words:

if (imgopened == "")//=== if no card is turned
{
    //do X, most likely: turn card
}
else
{
    //do Y
}

This could've been written as

if (!imgopened)
//or
if (imgopened == false)//falsy, but somewhat confusing
//or
if (imgopened == 0)//!confusing, don't use
//or, my personal favorite
if (imgopened === '')
于 2012-10-10T07:38:34.830 回答