多个文件中的 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2439204/
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
JavaScript in multiple files
提问by vijay.shad
I Have created two JavaScript files.One file is "validators.js" and other is "UserValidations.js".
我创建了两个 JavaScript 文件。一个文件是“validators.js”,另一个是“UserValidations.js”。
Here is the code for validators.js
这是validators.js的代码
function isBlankString(value) {
if (value.replace(/\s/g, "") == "") {
return true;
} else {
return false;
}
}
In other js file I have defined function for validating user name like this.
在其他 js 文件中,我定义了像这样验证用户名的函数。
function validateUsername(element) {
var username = element.value;
if(value.replace(/\s/g, "") == ""){
//nothing to validate
return;
}else{
//validation logic
}
}
Now as it is obvious i should have used isBlankString(value) method to check string length. But I am clue less about how can i use the function defined in other file?
现在很明显我应该使用 isBlankString(value) 方法来检查字符串长度。但我对如何使用其他文件中定义的函数知之甚少?
采纳答案by Sarfraz
Make sure that you include your validators.jsfile first on the page:
确保validators.js首先在页面上包含您的文件:
<script src="validators.js"></script>
Now you can use the function of that normally:
现在你可以正常使用它的功能了:
function validateUsername(element) {
var username = element.value;
if(isBlankString(value)){
//nothing to validate
return;
}else{
//validation logic
}
}
回答by Pascal MARTIN
The file that provides the function must be included, in your HTML document, before the function is actually used.
在实际使用该功能之前,必须将提供该功能的文件包含在您的 HTML 文档中。
Which means that, to be sure, validators.jsshould be included before UserValidations.js:
这意味着,可以肯定的是,validators.js应该包括在之前UserValidations.js:
<script src="validators.js" type="text/javascript"></script>
<script src="UserValidations.js" type="text/javascript"></script>
回答by Justin Johnson
As a side note, your isBlankStringfunction overly verbose. Consider replacing it with the following
作为旁注,您的isBlankString函数过于冗长。考虑将其替换为以下内容
function isBlankString(value) {
return value.replace(/\s+/g, "").length == "";
// or even: return value.replace(/\s+/g, "").length == 0;
}
The ifstatement that you have is completely unnecessary.
if你的说法是完全没有必要的。
回答by davehamptonusa
More importantly, you are defining both of your functions in the window object. Its best to globally protect your functions by creating your own namespace:
更重要的是,您在 window 对象中定义了两个函数。最好通过创建自己的命名空间来全局保护您的函数:
function isBlankString(value) {
if (value.replace(/\s/g, "") == "") {
return true;
} else {
return false;
}
}
you should put all of your objects in your own namespace like
你应该把你所有的对象放在你自己的命名空间中,比如
Sarfraz.isBlankString = function (value) {
if (value.replace(/\s/g, "") == "") {
return true;
} else {
return false;
}
}
As well as the other function. In reality, they are currently defined as window.isBlankString. If you load other people's javaScript into your page, you are much more likely to be overwritten.
以及其他功能。实际上,它们当前被定义为 window.isBlankString。如果您将其他人的 javaScript 加载到您的页面中,您很可能会被覆盖。
To your main question, both should be loaded before you execute any. Look into using jQuery and the $(document).ready functionality.
对于您的主要问题,两者都应该在您执行任何之前加载。研究使用 jQuery 和 $(document).ready 功能。
Always load your scripts first, then execute. Loading and executing simultaneously invites disaster and is usually not necessary. That way, you don't need to worry about which order you place them in the file.
始终先加载脚本,然后执行。同时加载和执行会招致灾难,通常没有必要。这样,您无需担心将它们放在文件中的顺序。
回答by Dave Hite
One nice method is to develop in multiple js files and then combine them together in your make/build system to create a JavaScript Library.
一种不错的方法是在多个 js 文件中进行开发,然后在您的 make/build 系统中将它们组合在一起以创建一个 JavaScript 库。
See this post for further discussion and example makefile: Makefile to combine js files and make a compressed version
有关进一步讨论和示例 makefile,请参阅此帖子:Makefile to combine js files and make acompressed version
You can also compress and minify the javascript to further reduce the file size: See Compressed JavaScriptor Best JavaScript compressor
您还可以压缩和缩小 javascript 以进一步减小文件大小:请参阅压缩 JavaScript或最佳 JavaScript 压缩器
回答by stimms
So long as you have both files included you can just call it. JavaScript has no real concept of namespaces to worry about. You might consider combining the files into one as it saves on the number of http requests sent to the server.
只要包含这两个文件,就可以调用它。JavaScript 没有真正的命名空间概念需要担心。您可以考虑将这些文件合二为一,因为这样可以节省发送到服务器的 http 请求的数量。
回答by Artic
Just include both of files to your html page and call functions when you need it. Nothing difficult and hard. Or use two of this functions in one file
只需将这两个文件都包含到您的 html 页面中,并在需要时调用函数。没有什么困难和困难。或者在一个文件中使用两个这个函数
回答by Vasily Korolev
I you are in an HTML file just include both files in your HTML.
如果您在 HTML 文件中,只需将这两个文件都包含在您的 HTML 中。

