我对JavaScript很新,但我是一名长期的程序员,我无法理解正在发生的事情。
我有这个:
var MainTable = {
"k1": 0,
"k2": 9,
"k3": 20
};
当我想访问特定元素时:
var index = "k1";
var value = MainTable[index];
现在,由于一个完全逃避我的原因,它每次都会返回undefined而不是0。我现在已经在这里待了大约2个小时,而我却得不到它。
谢谢!
编辑完整代码:
// These shouldn't be modified
var maxEPoints = 29;
var maxGold = 50;
var hudBarWidth_HR = 650; // 650 is for hr (px)
var hudBarWidth_LR = 350;
var IN_COMBAT = false;
var IN_COMBAT_WAITING = false;
var WolfDamageTable =
{
"m11_1": 99,
"m11_2": 99,
"m11_3": 8,
"m11_4": 8,
"m11_5": 7,
"m11_6": 6,
"m11_7": 5,
"m11_8": 4,
"m11_9": 3,
"m11_10": 0,
};
var EnemyDamageTable =
{
"m11_1": 0,
"m11_2": 0,
"m11_3": 0,
"m11_4": 0,
"m11_5": 1,
"m11_6": 2,
"m11_7": 3,
"m11_8": 4,
"m11_9": 5,
"m11_10": 6,
};
function ComputeCombatDamage( aPoints, ePoints, skill, endurance )
{
var ratio = ( aPoints - parseInt( skill ) ).toString();
var absRatio = ( aPoints - parseInt( skill ) );
var randVal = GIMME_A_RANDOM().toString();
$( "td#tableWolfSkill" ).html( aPoints.toString() );
$( "td#tableWolfEndurance" ).html( ePoints.toString() );
$( "td#ratio" ).html( ratio.toString() + " / " + randVal.toString() );
// ... code where I process the string index
var eDamage = EnemyDamageTable[eIndex]; // -> problem
var wDamage = WolfDamageTable[wIndex];
var wolfEndurance = ePoints - wDamage;
var enemyEndurance = endurance - eDamage;
var barWidth = ( wolfEndurance / maxEPoints ) * hudBarWidth_HR;
IN_COMBAT = wolfEndurance > 0 && enemyEndurance > 0;
return IN_COMBAT;
}
/*********************************************************************
* custom function to initiate the battle sequence
*********************************************************************/
function LoadBattle()
{
IN_COMBAT = true;
var aPoints = 0;
var ePoints = 0;
var gold = 0;
var name = $( "span#enemyName" ).html();
var skill = $( "span#enemySkill" ).html();
var endurance = $( "span#enemyEndurance" ).html();
if( skill.length == 0 ||
endurance.length == 0 )
{
alert( "Content is not properly formated for combat!" );
return;
}
window.location = "#fight_start";
// Set up the table before showing it
$( "td#tableEnemyName" ).html( name );
$( "td#tableEnemySkill" ).html( skill );
$( "td#tableEnemyEndurance" ).html( endurance );
var keepGoing = true;
while( IN_COMBAT )
{
if( typeof( Storage ) !== "undefined" )
{
aPoints = localStorage.aPoints;
ePoints = localStorage.ePoints;
}
keepGoing = ComputeCombatDamage( aPoints, ePoints, skill, endurance );
//endurance = $( "td#enemyResultEndurance" ).html();
if( keepGoing == false )
{
break;
}
//WaitCombat();
}
}
对我而言,错误似乎来自
var eDamage = EnemyDamageTable[eIndex];
var wDamage = WolfDamageTable[wIndex];
在function ComputeCombatDamage
我现在尽量减少它。
答案 0 :(得分:1)
wIndex
和eIndex
未定义。我可以详细说明,但应该说明一切。