0
我有一個功能,將skill button
添加到我創建的遊戲中。我希望在遊戲啓動時做的一件事是在我的INIT對象中添加一些功能。但是,因爲我在調用INIT和這些函數之前實際函數被JavaScript讀取,它會引發錯誤。現在我可以放棄我的代碼了,但我不喜歡我的代碼懸而未決。我希望它整齊整齊。有沒有辦法來解決這個問題?JS:在函數調用之前添加函數,如何不接收錯誤?
這是我我指的功能:
function addSkillButton(object) {
var button = document.createElement("button");
var target = dom.el("skillListWrapper");
button.textContent = object.name;
button.setAttribute("id", object.name);
button.setAttribute("class", "selection");
switch(object.category){
case "Attack":
target.insertBefore(button, target.children[0]);
break;
case "Defend":
target.insertBefore(button, target.children[1]);
break;
case "Healing":
target.insertBefore(button, target.children[2]);
break;
case "Debuff":
target.insertBefore(button, target.children[3]);
break;
default:
console.log("addSkillButton() - Case Statement has switched to default");
}
}
然後右鍵低於此功能是這樣的:
addSkillButton(strike);
addSkillButton(defend);
addSkillButton(cure);
addSkillButton(debuff);
我想這4個功能添加到我的Game.Init對象,所以它看起來更乾淨,它代表遊戲開始時將會加載4個按鈕。但是這樣做會給我一個錯誤。有沒有辦法解決它?
game = {
init: function() {
// Game.Start
// Log the messages to the chatbox and create buttons for users to select avatars.
logMessage("Select your Avatar.");
createButton("dwarfButton", "DWARF");
logMessage("4 Upgrade Points - 0 Skill Points.");
logMessage("Heavy Armor, More Balance")
createButton("dwarfButton", "HUMAN");
logMessage("2 Upgrade Point - 1 Skill Point.");
logMessage("Low Armor, Medium agility")
createButton("dwarfButton", "ELF");
logMessage("0 Upgrade Points - 2 Skill Points.");
logMessage("No Armor, High agility")
listBossStats(angel);
listPlayerStats(nullman);
addSkillButton(strike);
addSkillButton(defend);
addSkillButton(cure);
addSkillButton(debuff);
}
包裝在anon/iife中,調用內聯函數,然後返回inner – dandavis