[英]Is it possible to declare a variable in one javascript file and use it in another one?
If I had one javascript file : 如果我有一个javascript 文件 :
var myVariable = "Awesome variable";
and another javascript file : 和另一个javascript 文件 :
function printMyVariable() {
document.writeln(myVariable);
}
would the printMyVariable
method be able to recognize myVariable? printMyVariable
方法能够识别myVariable吗? My guess in "No", because the myVariable
scope isn't recognizable in the second javascript file. 我的猜测是“否”,因为myVariable
范围在第二个javascript文件中无法识别。 So, I was wondering if someone could explain to me what needs to be done to make the magic happen, if possible. 所以,我想知道是否有人可以向我解释如果可能的话,需要做些什么来使魔术发生。 :) :)
Thanks. 谢谢。
Yes, as long as the file with the variable declaration is included before the file that uses it since it's all parsed in the same chunk but in order. 是的,只要具有变量声明的文件包含在使用它的文件之前,因为它在同一个块中按顺序解析。
This is an exceptionally bad practice though. 但这是一种非常糟糕的做法。
It is possible since myVariable will be defined @ global scope though its in a different file. 这是可能的,因为myVariable将被定义为@ global scope,尽管它位于不同的文件中。 However make sure printMyVariable function is called after the variable is defined (in terms of including the script tags.) 但是,确保在定义变量后调用printMyVariable函数(就包含脚本标记而言)。
JS has flat scope, there are global 1 and local only. JS具有平坦的范围,仅有全局1和本地。 var
uses current scope. var
使用当前范围。 Let var foo
be in global scope -- you will get global variable assessible from any of files (there are no namespaces or modules). 让var foo
在全局范围内 - 您将从任何文件(没有名称空间或模块)获得全局变量。
There is a Global
object, and the global var becomes a property of it. 有一个Global
对象,全局var成为它的属性。 In the browser environment window
implements Global
, so your global var will have qualified name as window.foo
. 在浏览器环境window
实现Global
,因此您的全局var将具有限定名称window.foo
。
/*
assuming browser environment
execution flow: top to bottom
first file: (actually doesnt matter, becase its flat)
*/
var foo = "bar";
// equivalent to
window.foo = "bar";
// second file:
var foo = 42;
// redeclared? no, because equivalent statement is
window.foo = 42;
1 illustrative purpose only, see the second part. 只有1个说明的目的,请参阅第二部分。
As long as you call the function in the second file after you include the first file, you should be fine. 只要在包含第一个文件后在第二个文件中调用该函数,就应该没问题。 You introduce a global variable, which is assigned to the window
object in the DOM, so after you include the first file, window.myVariable
will be equal to "Awesome variable"
. 您引入了一个全局变量,该变量分配给DOM中的window
对象,因此在包含第一个文件后, window.myVariable
将等于"Awesome variable"
。 As mentioned above, though, all of this is a very bad idea. 如上所述,所有这些都是一个非常糟糕的主意。
As an old perlie, I would never use a variable starting with 'my' as a global variable - my = local in perl :-) I agree it's bad practice. 作为一个古老的perlie,我永远不会使用以'my'开头的变量作为全局变量 - 我的= perl in local :-)我同意这是不好的做法。
The way to think of this is not as seperate files, but as one big file of al the JavaScript files concatenated in order. 想到这个的方式不是单独的文件,而是作为JavaScript文件的一个大文件按顺序连接。 The scope is the same as it would be in that file. 范围与该文件中的范围相同。 Indeed, this is exactly what happens when you minify... 实际上,这正是当你缩小时发生的事情......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.