Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:如何从闭包全局化变量_Javascript_Jquery - Fatal编程技术网

javascript:如何从闭包全局化变量

javascript:如何从闭包全局化变量,javascript,jquery,Javascript,Jquery,我正在尝试编写一个全局服务,并希望在页面上的所有脚本中公开全局变量。 例如:MyService变量应该在所有脚本中都可用。这就像jQuery一样,页面上的所有脚本都可以使用它 我在看jQuery3.1.1源代码 ( function( global, factory ) { "use strict"; if ( typeof module === "object" && typeof module.exports === "object" ) { // For Co

我正在尝试编写一个全局服务,并希望在页面上的所有脚本中公开全局变量。 例如:MyService变量应该在所有脚本中都可用。这就像jQuery一样,页面上的所有脚本都可以使用它

我在看jQuery3.1.1源代码

( function( global, factory ) {

"use strict";

if ( typeof module === "object" && typeof module.exports === "object" ) {

    // For CommonJS and CommonJS-like environments where a proper `window`
    // is present, execute the factory and get jQuery.
    // For environments that do not have a `window` with a `document`
    // (such as Node.js), expose a factory as module.exports.
    // This accentuates the need for the creation of a real `window`.
    // e.g. var jQuery = require("jquery")(window);
    // See ticket #14549 for more info.
    module.exports = global.document ?
        factory( global, true ) :
        function( w ) {
            if ( !w.document ) {
                throw new Error( "jQuery requires a window with a document" );
            }
            return factory( w );
        };
} else {
    factory( global );
}

// Pass this if window is not defined yet
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
以上是从一开始的一小部分

这就是jQuery
var
声明的地方

var
version = "3.1.1",

// Define a local copy of jQuery
jQuery = function( selector, context ) {

    // The jQuery object is actually just the init constructor 'enhanced'
    // Need init if jQuery is called (just allow error to be thrown if not included)
    return new jQuery.fn.init( selector, context );
},
据我所知,这是在一个关闭我试图做类似的事情

(function (jQuery) {
    var MyService = function() {
        console.log('hello world from service');
    }
})(jQuery);
如果我在html页面中包含上述脚本并尝试访问MyService javascript解释器,则会抛出错误
MyService未定义
,这是正确的,因为在闭包中定义了该错误

要解决这个问题,我必须在闭包之外声明
var MyService
,我不想这样做

我的问题是

  • 我怎样才能达到预期的结果
  • 为什么jQuery没有失败

  • 试试这个;)

    可以通过如下方式设置窗口对象的属性来实现:


    window['hello']='world'

    这不是结束。这是自调用函数。 另一种方式

    var globalVar = {};
    (function (jQuery, g) {
    g.MyService = function() {
        console.log('hello world from service');
    }
    })(jQuery, globalVar);
    
    无需在
    窗口中更改任何内容

    // Output
    
    globalVar.MyService()
    hello world from service
    

    1:删除单词
    var
    或2使用
    window.MyService=
    您错过了jQuery源代码中全局公开的部分:
    window.jQuery=window.$=jQuery@Ritesh我不知道你的意思。我之前的评论是关于:
    为什么在jQuery的情况下它没有失败
    @A.Wolff得到它忽略以前的评论。这样,MyService被定义为windows对象上的属性,比我必须使用
    window的任何地方都要多。MyService
    ,但我们没有使用
    window。jQuery
    要使用jQueryYou可以同时使用
    MyService
    window.MyService
    。。。JQuery也一样,请尝试
    window.JQuery
    它可以工作;)知道了。很好用。
    // Output
    
    globalVar.MyService()
    hello world from service