简体   繁体   English

是否可以在一个javascript文件中声明一个变量并在另一个javascript文件中使用它?

[英]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函数(就包含脚本标记而言)。

simple 简单

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在全局范围内 - 您将从任何文件(没有名称空间或模块)获得全局变量。

further 进一步

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

"redeclaring" “重新声明”

/* 
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.

相关问题 如何在一个函数中声明全局变量赋值并在javascript中的另一个函数中使用 - how to declare a global variable assign value in one function and use in another function in javascript javascript中的全局变量(从一个文件中声明它,从另一个文件中放入值,然后从另一个文件中访问它) - Global variable in javascript (declare it from one file, put value from another file and accessing it from some another file) JavaScript 声明一个变量并在一个语句中使用逗号运算符? - JavaScript declare a variable and use the comma operator in one statement? 在另一个 javascript 文件中引用变量 - Referencing variable from one javascript file in another JavaScript修改一个文件中的变量并在另一个文件中读取它 - JavaScript modifying a variable in one file and reading it in another 在javascript中,在for循环中声明多个变量 - in javascript, declare more than one variable in for loop 将变量从一个 javascript 获取到另一个 php 文件中的另一个 javascript - Get variable from one javascript to another javascript in another php file 使用在一个javascript文件中创建的变量以在节点文件和另一个角度文件中使用 - To use a variable created in one javascript file to be used in a node file and another angular file javascript:使用另一个文件中的一个文件中的变量 - javascript: use variable from one file in other 在JavaScript文件外部声明变量,然后在文件中使用它 - Declare variable outside of javascript file and use it in file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM