Javascript javascript中的全局函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29514382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global functions in javascript
提问by ugandajs
I'm new to js and trying to understand global and private functions. I understand global and local variables. But if I have an html named test.htmland a 2 js files named test1.jsand test2.js. Now I include the test1.jsand test2.jsin test.htmland call the functions written in test2.jsinside test1.jsand test.html.
我是 js 的新手,并试图了解全局和私有函数。我了解全局和局部变量。但是,如果我有一个名为的HTMLtest.html和命名为2个js文件test1.js和test2.js。现在,我包括test1.js与test2.js中test.html并调用写在函数test2.js内部test1.js及test.html。
The functions that I have written in test2.js are in this form
我在test2.js中写的函数都是这种形式
function abc(){...}
function pqr(){...} etc.
are these above functions global? If they are , how can I not make them global and still access them in test1.jsand test.html?
以上这些功能是全局的吗?如果是,我怎么能不让它们成为全球性的并且仍然可以在test1.js和 中访问它们test.html?
As I have read global functions or global variables are bad right?
正如我读过的全局函数或全局变量是坏的对吗?
回答by Marko Gre?ak
Everything in JS is bound to containing scope. Therefore, if you define a functiondirectly in file, it will be bound to windowobject, i.e. it will be global.
JS 中的所有内容都绑定到包含范围。因此,如果您function直接在文件中定义 a ,它将绑定到window对象,即它是全局的。
To make it "private", you have to create an object, which will contain these functions. You are correct that littering global scope is bad, but you have to put something in global scope to be able to access it, JS libraries do the same and there is no other workaround. But think about what you put in global scope, a single object should be more than enough for your "library".
要使其“私有”,您必须创建一个包含这些功能的对象。你是正确的,乱扔全局范围是不好的,但是你必须在全局范围内放置一些东西才能访问它,JS库做同样的事情,没有其他解决方法。但是考虑一下您在全局范围内放置的内容,单个对象对于您的“库”来说应该绰绰有余。
Example:
例子:
MyObject = {
abc: function(...) {...},
pqr: function(...) {...}
// other functions...
}
To call abcfor somewhere, be it same file or another file:
要调用abc某个地方,无论是同一个文件还是另一个文件:
MyObject.abc(...);
回答by fellak
var SomeName = function() {
var function1 = function (number) {
return number+1;
}
var anotherFunction = function (number) {
return number+2;
}
return {
function1: function (number) {
return function1(number);
},
function2: function (number) {
return anotherFunction(number);
}
}
}();
calling
打电话
console.log(SomeName.function1(1)); //logs 2
console.log(SomeName.function1(1)); //日志2
console.log(SomeName.function2(1)); //logs 3
console.log(SomeName.function2(1)); //日志3
回答by Christian Juth
Anything defined in a file without any sort of wrapper will be bound to the window object. Anything bound to the window object is global.
在没有任何包装的文件中定义的任何内容都将绑定到 window 对象。任何绑定到 window 对象的东西都是全局的。
Example:
例子:
//these are global variables
foo = 123;
var ABC = 'school';
//these are "private" variables
test = function(){
var foo = 123
}
(function(){
var ABC = 'school';
}).call(this);
Since global variables in every file will be part of the windowobject, you can access them between files. It is important when creating "private" variables you add var. This says override any global variables in the current "wrapper". If I have a global variable fooand I define it again in a function with varthey will be separate.
由于每个文件中的全局变量都是window对象的一部分,因此您可以在文件之间访问它们。创建您添加的“私有”变量时很重要var。这表示覆盖当前“包装器”中的任何全局变量。如果我有一个全局变量foo并且我在一个函数中再次定义它,var它们将是分开的。
var foo = 123;
(function(){
var foo = 987; //this foo is separate from the above foo
}).call(this);
If you do have a "wrapper" and you want to define a global function you can do it like this:
如果你有一个“包装器”并且你想定义一个全局函数,你可以这样做:
window.foo = 123;
(function(){
window.foo = 123;
}).call(this);
Both functions will do the same thing.
这两个函数都会做同样的事情。
Personally, I prefer to put everything in a wrapper and only define global variables when I need them using window.
就个人而言,我更喜欢将所有内容都放在一个包装器中,并且仅在需要时使用window.
(function(){
//all code goes here
//define global variable
window.foo = 123;
})call(this);
回答by hasen
If you don't understand why global variables are bad, then why are you trying to avoid them?
如果您不明白为什么全局变量不好,那么您为什么要避免使用它们?
Global functions aren't necessarily bad. What's bad is state that anyone and anything and change.
全局函数不一定是坏的。不好的是,任何人和任何事物都会发生变化。
In general since you're new to Javascript, it's fine to start out with global functions spread out across multiple javascript files that you include in your html file via script tags.
一般来说,由于您是 Javascript 的新手,因此可以从分布在您通过脚本标签包含在 html 文件中的多个 javascript 文件中的全局函数开始。
As you transition from beginner to intermediate, you will have to look into some "module" solution (I personally recommend RequireJS).
当您从初学者过渡到中级时,您将不得不研究一些“模块”解决方案(我个人推荐RequireJS)。
For now though, you can make do with a simpler module pattern:
不过现在,您可以使用更简单的模块模式:
var T1 = function() {
return < some module object >
})(); // notice the parenthesis
Google "Javascript module pattern".
谷歌“Javascript 模块模式”。
Also see this answer.
另请参阅此答案。
回答by Sohaib
in test2.js you can write this to make the function global
在 test2.js 中,您可以编写此代码以使函数全局化
window.abc = function(){...}
and then in test1.js yo can access it like this
然后在 test1.js 中你可以像这样访问它
window.parent.abc();
I hope it will help you
我希望它会帮助你

