简体   繁体   English

如何使我的功能在全局范围内可见

[英]How do I make my function visible in the global scope

I have following javascript to make function $E 我有以下javascript使功能$E

On window.onload it throws error that $E is not defined. window.onload ,抛出未定义$E错误。

So my question is how do I make $E visible in the global scope so I can access it outside the (function(){})(); 所以我的问题是如何使$E在全局范围内可见,以便可以在(function(){})();之外访问它(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);

To make a variable visible in the global scope, set it on the window object. 要使变量在全局范围内可见,请在window对象上进行设置。 For example: 例如:

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

Javascript : How to create global functions & variables Javascript:如何创建全局函数和变量

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

You should set a window variable to your function to make it accessible. 您应该为function设置一个window变量以使其可访问。 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 https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Functions_and_function_scope

javascript function scope javascript功能范围

This made the function global from within another function when tested: 经过测试,这使得该函数从另一个函数内部成为全局函数:

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

test();
$E();

http://jsfiddle.net/WEg4b/ 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);

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. 通过在模块内部声明所有这些代码,您可以创建一个与所有其他代码隔离的新作用域,因此,如果您希望在全局上下文中使用它,只需在该函数外部声明它即可。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM