【问题标题】:How do I make my function visible in the global scope如何使我的函数在全局范围内可见
【发布时间】:2013-01-05 06:13:04
【问题描述】:

我有以下javascript来制作函数$E

window.onload 上会抛出$E 未定义的错误。

所以我的问题是如何使$E 在全局范围内可见,以便我可以在(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);

【问题讨论】:

  • 为什么你的写作function $E(){}insite (function(){});write outside

标签: javascript anonymous-function


【解决方案1】:

要使变量在全局范围内可见,请将其设置在 window 对象上。例如:

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

【讨论】:

  • 我仍然无法访问它
【解决方案2】:

Javascript : How to create global functions & variables

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

您应该为您的function 设置一个window 变量以使其可访问。这些链接会有所帮助。 您也可以在外部定义函数并将其设为全局。

详细研究,

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

javascript function scope

【讨论】:

    【解决方案3】:

    这使得该函数在测试时在另一个函数中成为全局函数:

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

    http://jsfiddle.net/WEg4b/

    所以,为了适应您的需求:

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

    【讨论】:

    • 为什么在写function abc () { ... } 时不让它成为全局的并且abc() 可用?
    【解决方案4】:

    只需在匿名函数之外声明它。通过在模块内声明所有代码,您将创建一个新范围,与所有其他代码隔离,因此,如果您希望它在全局上下文中可用,只需在该函数之外声明它。

    【讨论】:

      【解决方案5】:

      尝试改变函数声明的顺序

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-20
        • 2012-07-05
        • 2013-12-12
        • 2012-02-18
        • 2012-09-08
        • 1970-01-01
        • 2021-01-01
        • 2013-07-13
        相关资源
        最近更新 更多