homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
the script that knew too much
lucy24




msg:4650742
 5:08 pm on Mar 3, 2014 (gmt 0)

Today's minor head-scratcher:

I've got code that depends on checking the values returned by other functions, like
if testFoo()
{ do stuff }

and so on. Out of the blue, one function starts behaving as if the value returned by the test function is always true (or non-zero, or something-- I don't generally specify). Closer inspection reveals that it's making this decision without even going to the test function.

Still closer inspection reveals that I'd inadvertently given the test function twice. Not two functions with the same name: two paste-ins of the same function. Removing the duplicate solved the problem. (As I post this, I now realize that I don't know whether the script was visiting the other copy of the function, since I didn't then know it existed.)

Question: Can someone explain in words of two syllables what was happening under the hood?

 

Fotiman




msg:4650748
 5:25 pm on Mar 3, 2014 (gmt 0)


if testFoo()
{ do stuff }

I assume you meant it looked like this:

if (testFoo())
{ do stuff }



Still closer inspection reveals that I'd inadvertently given the test function twice

As in something like this?

function testFoo() {
// do some tests
}
function testFoo() {
// do some tests
}


In theory, that shouldn't have caused the behavior you describe. I can't think of anything that would cause that, unless you had done something like this:


if (testFoo) // missing the () after testFoo

The above test would basically always return true without executing testFoo (since the testFoo function was defined, testFoo would act "truthy" when used in a boolean expression).

lucy24




msg:4650894
 5:28 am on Mar 4, 2014 (gmt 0)

missing the () after testFoo

Ordinarily I would say that is all too likely, because it's just the sort of thing I would do. But this time I'd been converting existing code, including a global round of
fn (\w+)
>> CHANGE TO >>
\1()

So it's not very likely. Always possible though.

Elsewhere:
var gotit = ;
var gotit = false;
rest-of-function here


This is a pretty blatant typo and you would expect it to result in some sort of error condition. You would not expect it to result in the failure of just two things: reading cookies and addEventListener. Well, maybe you would expect it, but I wouldn't. Huh.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved