Arron S
Arron S

Reputation: 5537

Javascript: This evaluates to true and false at the same time! What is going on?

Does anyone have an explanation of how javascript could do this. Somehow this function is both true and false at the exact same time. This is just one pass, no looping or anything.

  console.log(key);
    if (this.watches.get(key)) {
      console.log("found it");
    } else {
      console.log("whhhat?");
    }
    console.log(this.watches);

Firebug Console logs as is.

search-key
found it
Object search-key=Object $family=Object
whhhat?
Object search-key=Object $family=Object

[EDIT] Here it is. The full script and the block of output above is copy and paste from firebug. This is the strangest thing I have ever seen. http://snipt.org/Hkl

I use mootools framework so the this.watches = $H({}); is a hashtable. I was using an Array and was experiencing the exact same issue, then switched it to $H({}) because I thought i was doing something wrong.

Upvotes: 1

Views: 755

Answers (3)

paxdiablo
paxdiablo

Reputation: 881543

Change your code snippet to this to see if it's actually being called twice but, for some reason, the console.log(key) isn't outputting something the second time around.

console.log("=====");
console.log(key);
if (this.watches.get(key)) {
  console.log("found it");
} else {
  console.log("whhhat?");
}
console.log(this.watches);

Then you'll get the output:

=====
search-key
found it
Object search-key=Object $family=Object
=====
whhhat?
Object search-key=Object $family=Object

If you get the output:

=====
search-key
found it
Object search-key=Object $family=Object
whhhat?
Object search-key=Object $family=Object

then I'm mistaken and I'll remove the answer (or more likely make it community wiki so others won't follow the same path).

Upvotes: 0

hasen
hasen

Reputation: 166162

If this

Object search-key=Object $family=Object

is produced by:

console.log(this.watches);

Then this is obviously not just one pass.

This is not to say that it's in a loop or anything, just that this code is being called more than once.

One might say

why is search-key showing up only once then?

The answer is: we don't really know because we don't see all the code.

The most likely scenario is that key in the second time is the empty string

try console.log("") it prints nothing

also try this:

>>> console.log(""); console.log(1); console.log(""); console.log(2)

copy the output and paste it in any text editor (i.e. paste it as plain text)

1
2

it looks like there was nothing between 1 and 2, even though the console does show a cue of an empty line, but this cue disappears when you copy-paste the output as plain text.

UPDATE

well, if this process is initiated by the call to WCHistory.implement(..), and this method is being called twice, then obviously, the second time, for some reason, the key is empty.

Try this

Change console.log(key) to console.log("key: " + key) and you should see something like this:

key: search-key
found it
Object search-key=Object $family=Object
key: 
whhhat?
Object search-key=Object $family=Object

Upvotes: 9

RedBlueThing
RedBlueThing

Reputation: 42522

I think the answer is that this isn't just one pass.

This looks like the code is getting called twice, then second time with an empty key. Probably need to see more of the code to be sure.

I guess you could stick a integer count incremented before the if statement to confirm this hypothesis.

Upvotes: 1

Related Questions