Html 来自另一个外部 javascript 文件的外部 Javascript 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18582150/
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
External Javascript calling functions from another external javascript file
提问by ProgrammingFreak
So, I've looked at other questions like this and they haven't really answered the questions.
所以,我看过其他类似的问题,但他们并没有真正回答这些问题。
In short: I've worked with HTML, Java, VB, and whatever language MIRC uses(it's been a little under 10 years).
简而言之:我使用过 HTML、Java、VB 以及 MIRC 使用的任何语言(已经不到 10 年了)。
I'm new to javascript, but because of my other computer language know-how's... I know that using Java, I can create class's and class-like functions.
我是 javascript 新手,但由于我的其他计算机语言专业知识......我知道使用 Java,我可以创建类和类类函数。
But, with html/javascript, I'm not sure how to call a function from one external .js file to another.
但是,使用 html/javascript,我不确定如何将一个函数从一个外部 .js 文件调用到另一个。
Let's say: I want to make a player class that I can call functions from anytime and use it in the 'main' .js file that uses those functions from that class inside another function. For example.
比方说:我想创建一个播放器类,我可以随时调用它的函数,并在“main”.js 文件中使用它,该文件在另一个函数中使用该类中的这些函数。例如。
If I had
如果我有
function isDead() {
if (health < 10 || energy < 10)
{
return dead = 'true';
}
else { return dead; }
}
in Player .js file, and in the main one, I had.
在 Player .js 文件中,在主要文件中,我有。
function fight() {
if (isDead() == 'true')
{
energy -= 10;
health -= 10;
Swords();
}
document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;
}
In the item.js I had
在 item.js 我有
var swordDamage = 0;
function Swords(){
if (swordDamage < 3)
swordDamage +=1;
else
return sword -=1;
}
(Mind you this actually worked (I haven't tested the swords() yet as I just made it for this question.)
(请注意,这确实有效(我还没有测试过 Swords(),因为我刚刚为这个问题做的。)
Is there any particular way I need to call upon the functions from other .js files into the main.js file.
有什么特别的方法我需要将其他 .js 文件中的函数调用到 main.js 文件中。
example: main.function();
示例:main.function();
or am I better off saving them as an html document and writing nothing but script to get around this?
还是我最好将它们保存为 html 文档并只编写脚本来解决这个问题?
Thanks for answering!
谢谢回答!
回答by Claudio Santos
I come from the same environment that you came. I did the javascript course that helped me a lot, codeacademy.com.
我和你来自同一个环境。我参加了对我有很大帮助的 javascript 课程,codeacademy.com。
Well I should implement your sample like this:
好吧,我应该像这样实现您的示例:
fighter.js
战斗机.js
function Fighter(){
this.health = 100;
this.energy = 100;
this.isDead = function() {
if (health < 10 || energy < 10)
{
return dead = 'true';
}
else {
return dead;
}
}
this.Punch = function(otherFighter){
otherFighter.energy -= 10;
otherFighter.health -= 10;
}
this.AtackWithItem(item, fighter){
otherFighter.energy -= item.damage;
otherFighter.health -= item.damage;
}
}
main.js
主文件
function initFight() {
var fighterA = new Fighter();
var fighterB = new Fighter();
document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;
fighterB.punch(fighterA);
document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;
var sword = new item();
fighterA.AtackWithItem(sword,fighterB);
document.getElementById("energy").innerHTML = energy;
document.getElementById("health").innerHTML = health;
document.getElementById("sword").innerHTML=sword;
}
item.js
项目.js
function item(){
this.type = "Swords"
this.damage = 20;
}
All files fighter.js, main.js and item.js will be refer with the tag:
所有文件 fighter.js、main.js 和 item.js 都将使用标签引用:
<script src="../item.js"></script>
<script src="../fighter.js"></script>
<script src="../main.js"></script>
in the index.html.
在 index.html 中。
Hope this help you...
希望这对你有帮助...