1

I have following javascript to make function $E

On window.onload it throws error that $E is not defined.

So my question is how do I make $E visible in the global scope so I can access it outside the (function(){})(); function

window.onload = function() {
   $E("bhavik").warn();
}
(function() {
                function $E(s) {
                    return new ge(s)
                }
                function ge(sel) {
                    this.arg = sel;
                    return this;
                }
                ge.proto = ge.prototype = {warn: function() {
                        alert(this.arg)
                    }};
                ge.proto.hi=function(){alert("hi "+this.arg)}
                $E("bhavik").hi();
            })(window);
Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
Bhavik Patel
  • 789
  • 6
  • 20

5 Answers5

3

To make a variable visible in the global scope, set it on the window object. For example:

function $E(s) {
   return new ge(s);
}
window.$E = $E;
N Rohler
  • 4,595
  • 24
  • 20
1

Javascript : How to create global functions & variables

http://www.w3schools.com/jsref/jsref_obj_global.asp

You should set a window variable to your function to make it accessible. These links will help. Also you can define function outside and make it global.

For detailed study,

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope

javascript function scope

Community
  • 1
  • 1
LPD
  • 2,833
  • 2
  • 29
  • 48
1

This made the function global from within another function when tested:

function test()
{
  window.$E = function() { alert('test'); };
}

test();
$E();

http://jsfiddle.net/WEg4b/

So, to adapt it to your needs:

(function() {

                function ge(sel) {
                    this.arg = sel;
                    return this;
                }

                window.$E = function $E(s) { return new ge(s); };

                ge.proto = ge.prototype = {warn: function() {
                        alert(this.arg)
                    }};
                ge.proto.hi=function(){alert("hi "+this.arg)}
                $E("bhavik").hi();
            })(window);
Alex W
  • 37,233
  • 13
  • 109
  • 109
0

Just declare it outside the anonymous function. By declaring all that code inside the module you are creating a new scope, isolated from all other code so, if you want that to be available in the global context, just declare it outside of that function.

Hugo
  • 6,244
  • 8
  • 37
  • 43
0

try to change the order of function declaration

(function() {
            function $E(s) {
                return new ge(s)
            }
            window.$E = $E;
            function ge(sel) {
                this.arg = sel;
                return this;
            }
            ge.proto = ge.prototype = {warn: function() {
                alert(this.arg)
            }};
            ge.proto.hi=function(){alert("hi "+this.arg)}
            $E("bhavik").hi();
        })(window);
window.onload = function() {
    $E("bhavik").warn();
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79