Make A javascript variable only avaliable to functions on the same .js file

喜夏-厌秋 提交于 2019-12-23 12:25:11

问题


I somewhat new to Javascript and I'm stuck on this one item. Can someone please show me how to make a javascript variable only usable on the .js file that it is on.

EXAMPLE:

Say I have two .js files, PG1 & PG2. PG1 contains var channel = Channel01;

PG2 contains a variable with the same name, but a different entered Variable (the part after the equals sign) (var channel = Channel02)

I don't want function1 on PG1 to use the variable on PG2 or for function2 on PG2 to use the variable on PG1.

I am also calling these functions from a seperate HTML page.

Problem: All of my functions end up using only one variable no matter what page they are on. (ex. function1 & function2 both use var channel = channel01)

Question: How can I limit functions on page1 to use only the variables on that .js page

Thanks!


回答1:


module pattern :

var myModule = (function(exports){

    var x = "foo";

    exports.myFunction = function(){
        alert(x);
    };

})(typeof exports!="undefined" && exports instanceof Object ? exports : window );

myFunction will be available in the window scope in the browser.

EDIT

i quote the author : "I am also calling these functions from a seperate HTML page."

so the author needs a global access to the functions he defines.

var myModule is not needed though , nor export , it is just for AMD compatibility :

(function(exports){

    var x = "foo";

    exports.myFunction = function(){
        alert(x);
    };

})(window);

now x for myFunction only exists in the closure , but myFunction can still access x in the global scope. any x definition in the global scope or whatever scope will not affect x = "foo"




回答2:


Wrap the whole thing in an Immediately Invoked Function Expression, which will effectively give the file its own scope.

(function() {
   // Your file's contents live here.
})();



回答3:


If you don't actually need to expose any of your variables to the global scope, you can wrap your entire JavaScript code in an immediately-invoked function expression or IIFE. Here, you define an anonymous function expression and immediately invoke it. The result is that instead of polluting the global scope with your variables, you keep them nice and tidy in the local scope of that function.

(function() {
    var channel = Channel01;

    // Put the rest of your PG1 code here, for example:

    function init() {
        channel.open();
    }

    init();
})();

You can then wrap your PG2 code in an IIFE in a similar fashion. The result will be that the two scripts share no variables other than the already defined global variables, such as Channel01 and Channel02.




回答4:


Although not a direct answer to your question, this is related and important, as missing it out could pollute other modules. If you dont use the var keyword the variable is on the global scope i.e

(function () {
   x = 'foo'; //x is on the global scope and can be seen everywhere
})()

(function () {
   var y = 'bar'; //y is local to this function
})()

I hope i havent duplicated what anyone has said above but i couldnt see it mentioned



来源:https://stackoverflow.com/questions/10644864/make-a-javascript-variable-only-avaliable-to-functions-on-the-same-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!