binding an object to the global scope

Posted by elduderino on Stack Overflow See other posts from Stack Overflow or by elduderino
Published on 2012-12-05T16:53:51Z Indexed on 2012/12/05 17:03 UTC
Read the original article Hit count: 186

Filed under:

I have the following code:

var myVar = (function (window) {    

    myobj = {};

    myobj.boo = function() {
        alert('hi');
    };

    window.myVar = myobj;

})(window);

myVar.boo();

Why don't I get back the alert when I call myVar.boo() ? I've created an anonymous self-executing function and fed in the window object. Inside that I have another object with a method assigned to it. I then assign the global myVar variable to this obj. This should provide an alias to the my myobj object. However when I call the function I get an Cannot call method 'boo' of undefined error

© Stack Overflow or respective owner

Related posts about JavaScript