简体   繁体   English

如何在不同的JS文件之间共享相同的作用域变量

[英]How to share same scope variables between different JS files

I have three script files. 我有三个脚本文件。 main.js, script1.js, script2.js. main.js,script1.js,script2.js。

The main.js just include that scripts into the document: main.js只是将该脚本包含在文档中:

function IncludeJs(sFileName) {
    document.writeln('<script type="text/javascript" src="' + sFileName + '"></script>');
}

IncludeJs("../script1.js");
IncludeJs("../script2.js");

The script1.js and script2.js code is under same namespace. script1.js和script2.js代码位于同一命名空间下。

script1.js: script1.js:

var sharedNamespace = sharedNamespace || {}; 

(function () {

    "use strict";

     function func1(){
        ....
     }

}).apply(sharedNamespace );

script2.js: script2.js:

var sharedNamespace = sharedNamespace || {}; 

(function () {

    "use strict";

     function func2(){
        return func1();
     }

}).apply(sharedNamespace );

The func2 is not working because func1 is undefined. func2不起作用,因为func1未定义。 How i can put script1.js and script2.js under same scope with shared variables? 我如何才能将script1.js和script2.js与共享变量置于同一范围内?

The solution of sharedNamespace.func1 = function(){} , is worst for me, because i don't want to expose this function to client who use my library... 对我来说, sharedNamespace.func1 = function(){}的解决方案最糟糕,因为我不想将此功能公开给使用我的库的客户端...

...i don't want to expose this function to client who use my library... ...我不想向使用我的图书馆的客户公开此功能...

If you're going to have separate script files and you want them to define functions that call each other, you have no choice. 如果要使用单独的脚本文件,并且希望它们定义相互调用的函数,则别无选择。 It's impossible to have script1.js and script2.js share a common scope that isn't also accessible to other scripts on the page. 不可能让script1.jsscript2.js共享一个公共范围,该范围也无法被页面上的其他脚本访问。

Instead, you'll probably want to have the separate files you use for development combined for deployment, because a single script can (of course) have private information. 相反,您可能希望将用于开发的单独文件合并以进行部署,因为单个脚本可以(当然)可以具有私有信息。 (Private unless the user edits the script, of course.) (当然,除非用户编辑脚本,否则为私人。)

So for instance, you might have something like this: 因此,例如,您可能会遇到以下情况:

script1.js : script1.js

var internal = internal || {};
(function(i) {
    i.func1 = function func1() {
        // ...code here, possibly using i.func2()
    };
})(internal);

script2.js : script2.js

var internal = internal || {};
(function(i) {
    i.func2 = function func2() {
        // ...code here, possibly using i.func1()
    };
})(internal);

When doing development, you include them separately and so any code could use internal.func1 . 在进行开发时,您将它们分别包含在内,因此任何代码都可以使用internal.func1 But then when doing your all-in-one-script build, you'd have it wrapped 但是,当您执行多合一脚本构建时,您需要将其包装

(function() {
    // ...contents of script1.js here...
    // ...contents of script2.js here...
})();

Now, internal is private to the combined script. 现在, internal对组合脚本是私有的。

That's off the top of my head; 那是我的头顶。 there are probably less cumbersome ways... 可能不那么麻烦

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

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