Javascript global object calls function?

Posted by Troels on Stack Overflow See other posts from Stack Overflow or by Troels
Published on 2010-12-31T15:40:09Z Indexed on 2010/12/31 15:53 UTC
Read the original article Hit count: 215

Hello stackoverflow

I have a very odd problem with javascript. My code is rather long so here is an example of the structure and the problem:


    var x = new function f() {
        this.id = "";
    }

    function g(obj) {

        if (x.id == "") {

            ...
            obj.firstChild.setAttribute("onclick", "javascript:o();");  
            ...
            x.id = obj.id;
        }
        else if (x.id != obj.id) {

            ...
            x.id = "";
            g(obj);
        }
    }

    function o() {

        ...

        if (something == something) {
            ...
        }
        else {
            ...
            x.id = ""; // if-statement of the g() function is called here?
        }
    }

As you can see, the if-statement of the g() function is for some reason called or re-run upon x.id being changed. I simply cannot understand this, because they are not in the same scope, and changing a variable should under no circumstances trigger anything?

Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about scope