Welcome to WebmasterWorld Guest from 54.227.52.24

Forum Moderators: open

Message Too Old, No Replies

the script that knew too much

     

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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

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

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month




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

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month