tags:

views:

301

answers:

4

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.

+1  A: 

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.

Cannonade
That's what I thought at first too, but the "search-key" is only being output once... Even if it was empty, it'd spit out a blank line to the log. Something about all this doesn't add up. :P
Daniel Lew
Agreed with Daniel, it really seems it isn't the whole code ...
eglasius
Would it still output a blank line if null is passed to the log?
Quintin Robinson
This is probably the answer, he just pasted the code incompletely.
Luca Matteis
Actually Luca, that wasn't his answer (is Daniel and I on the comments on the question ... the question is off, lets save the votes for the real stuff)
eglasius
I meant, this answer is about it being called twice, not about the code being uncomplete :)
eglasius
@Quintin: I just tested console.log() with both null and undefined, in both cases it outputs just that: "null" or "undefined".
Daniel Lew
Yeah then there must be code omitted.
Quintin Robinson
Yep, definitely need to see more code to determine the actual problem.
Cannonade
Safari 3's Web Inspector can render console.log('') as if there were nothing logged (there's a slight pixel shift, but when copying the text, it may not come through as a blank line).
eyelidlessness
Evidently so does Firebug.
eyelidlessness
A: 

Need to see more context. Is this within an event handler? What is 'this'?? why is this question tagged 'quantum'?

Scott Evernden
+6  A: 

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
hasen j
If it's two passes, then it is unexplained how "search-key" is only output once.
Daniel Lew
same answer as cannonade ... wrong btw, the code is surely incomplete i.e. search-key would appear 2 times (or undefined or whathever)
eglasius
Daniel, because the code is incomplete, we don't know how the console.log works, possibly it's not printing anything because there is no search key
hasen j
I just tested console.log() (see the comments in Cannonade's answer), and it'll output something no matter what. The code is incomplete, but that doesn't necessarily mean it's looping (though that's what I assume as well).
Daniel Lew
y, we didn't post answer because the code was clearly off ...
eglasius
daniel, try console.log("")
hasen j
Ah, that explains it then.
Daniel Lew
Then it would indicate that the code is most likely getting called more than once.
Quintin Robinson
Hasen j, ya, you were right. it was the console.log not logging anything on the second event trigger. stupid mistake on my part. thanks.
Arron
A: 

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).

paxdiablo