Javascript:从另一个文件调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11746955/
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 : calling function from another file
提问by hqt
I'm just new comer at Javascript so when I read a Javascript document, and there're many complex structures that I cannot follow.
我只是 Javascript 的新手,所以当我阅读 Javascript 文档时,有很多我无法理解的复杂结构。
Here is the short explanation of Javascript code that I'm reading : in my case there are two main files : Helper.js
and Circle.js
.
这是我正在阅读的 Javascript 代码的简短说明:就我而言,有两个主要文件:Helper.js
和Circle.js
.
In Helper.js, there is a method name :using:function(param1,param2)
. And below is code for Circle.js
:
在 Helper.js 中,有一个方法名称 : using:function(param1,param2)
。下面是代码Circle.js
:
Helper.using('py.Figures', function (ns) {
ns.Circle = function (params) {
// some additional methods and code here
}
ns.Alert = function(){ // for the test purpose
alert('hello');
}
});
And in file test.html, I write some code like this :
在文件 test.html 中,我写了一些这样的代码:
<script src="Helper.js"></script>
<script src="circle.js"></script>
<script>
test = function(){
py.Figures.Alert(); // calling for testing purpose
}
</script>
<body onload="test();"></body>
When I run on Chrome and view in console, I meet this error :
当我在 Chrome 上运行并在控制台中查看时,我遇到了这个错误:
Uncaught TypeError: Object # has no method 'Alert'
未捕获的类型错误:对象 # 没有方法“警报”
It means I haven't import those class, yet. Please tell me how to calling function from another file. In my case is : calling Alert()
这意味着我还没有导入这些类。请告诉我如何从另一个文件调用函数。在我的情况下是:打电话Alert()
Thanks :)
谢谢 :)
@ Edit: I have added some links for the code :
@编辑:我为代码添加了一些链接:
回答by Samuel Lopez
Why don't you take a look to this answer
你为什么不看看这个答案
Including javascript files inside javascript files
在 javascript 文件中包含 javascript 文件
In short you can load the script file with AJAX or put a script tag on the HTML to include it( before the script that uses the functions of the other script). The link I posted is a great answer and has multiple examples and explanations of both methods.
简而言之,您可以使用 AJAX 加载脚本文件或在 HTML 上放置一个脚本标记以包含它(在使用其他脚本功能的脚本之前)。我发布的链接是一个很好的答案,并且有两种方法的多个示例和解释。
回答by Mayank Vora
Yes you can. Just check my fiddle for clarification. For demo purpose i kept the code in fiddle at same location. You can extract that code as shown in two different Javascript files and load them in html file.
是的你可以。只需检查我的小提琴以进行澄清。出于演示目的,我将代码保存在同一位置的小提琴中。您可以提取该代码,如两个不同的 Javascript 文件中所示,并将它们加载到 html 文件中。
https://jsfiddle.net/mvora/mrLmkxmo/
https://jsfiddle.net/mvora/mrLmkxmo/
/******** PUT THIS CODE IN ONE JS FILE *******/
var secondFileFuntion = function(){
this.name = 'XYZ';
}
secondFileFuntion.prototype.getSurname = function(){
return 'ABC';
}
var secondFileObject = new secondFileFuntion();
/******** Till Here *******/
/******** PUT THIS CODE IN SECOND JS FILE *******/
function firstFileFunction(){
var name = secondFileObject.name;
var surname = secondFileObject.getSurname()
alert(name);
alert(surname );
}
firstFileFunction();
If you make an object using the constructor function and trying access the property or method from it in second file, it will give you the access of properties which are present in another file.
如果您使用构造函数创建一个对象并尝试从它访问第二个文件中的属性或方法,它将为您提供另一个文件中存在的属性的访问权限。
Just take care of sequence of including these files in index.html
只需注意在 index.html 中包含这些文件的顺序