如何在javascript中总结两个不同的功能

时间:2015-02-06 07:30:04

标签: javascript

我试图总结两个函数,它们是同一个对象的一部分。

var player = {
stats: 0,
level: 1,
inventory: 30,
baseStrength: 5,
weaponStrength: 0,
armorStrength: 0,
accessoryStrength: 0,
totalStrength: function () {
    return (player.baseStrength + player.weaponStrength + player.armorStrength + player.accessoryStrength);
},
baseEndurance: 5,
weaponEndurance: 0,
armorEndurance: 0,
accessoryEndurance: 0,
totalEndurance: function () {
    return (player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance);
},
baseAgility: 5,
weaponAgility: 0,
armorAgility: 0,
accessoryAgility: 0,
totalAgility: function () {
    return (player.baseAgility + player.weaponAgility + player.armorAgility + player.accessoryAgility);
},
baseDexterity: 5,
weaponDexterity: 0,
armorDexterity: 0,
accessoryDexterity: 0,
totalDexterity: function () {
    return (player.baseDexterity + player.weaponDexterity + player.armorDexterity + player.accessoryDexterity);
},
baseIntelligence: 5,
weaponIntelligence: 0,
armorIntelligence: 0,
accessoryIntelligence: 0,
totalIntelligence: function () {
    return (player.baseIntelligence + player.weaponIntelligence + player.armorIntelligence + player.accessoryIntelligence);
},
baseWisdom: 5,
weaponWisdom: 0,
armorWisdom: 0,
accessoryWisdom: 0,
totalWisdom: function () {
    return (player.baseWisdom + player.weaponWisdom + player.armorWisdom + player.accessoryWisdom);
},
baseLuck: 5,
weaponLuck: 0,
armorLuck: 0,
accessoryLuck: 0,
totalLuck: function () {
    return (player.baseLuck + player.weaponLuck + player.armorLuck + player.accessoryLuck);
},
experience: 0,
maxexperience: 100,
gold: 0,
health: 50,
maxhealth: function () {
    return ((player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance) * 10);
},
mana: 50,
maxMana: function () {
    return (player.totalWisdom * 10 + player.totalIntelligence * 5);
},
manaRegen: function () {
    return (player.totalWisdom);
},
mindamage: function () {
    return Math.floor((player.totalStrength * 0.5));
},
maxdamage: function () {
    return (player.totalStrength * 0.8);
},
hpregen: function () {
    return Math.floor((player.totalEndurance));
},
accuracy: function () {
    return Math.floor((95 + (player.totalAgility * 0.05 + player.totalLuck * 0.1)));
},
defense: function () {
    return (player.totalDexterity * 1.2 + player.totalEndurance * 0.5);
},
evasion: function () {
    return (5 + (player.totalAgility * 0.5 + player.totalLuck * 0.15));
},
criticalChance: function () {
    return (player.totalAgility * 0.5 + player.totalLuck * 0.5);
},
criticalDamage: function () {
    return (player.totalStrength * 0.1 + player.totalDexterity * 0.1);
},
Lifesteal: function () {
    return (player.totalIntelligence / 100);
},
Lifesteal2: function () {
    return (player.totalIntelligence);
},
dropRate: 0,
expRate: 0
};

好的,你可以看到,每个stat,我有5个对象属性,base / weapon / armor / accessory / total。总计是总结所有这些统计数据的函数。

之后我们有玩家maxhealth,伤害也是使用“总”属性计算的函数,但由于它也是一个功能我不能使它工作。所以我想出的唯一方法就是代替 (player.totalEndurance * 10)

我必须((player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance) * 10)

以及我在html中显示它的部分:

document.getElementById("maxhealth").innerHTML = player.maxhealth();

我添加()因为它是一个函数,但是它不起作用我得到NaN结果,函数未定义或者“对象不是函数”错误。

我希望你能帮助我:)谢谢

1 个答案:

答案 0 :(得分:2)

第2部分(最后):

要使用totalEndurance进行计算,您需要在通话中添加括号,因为它是一个功能。

player.totalEndurance()

第1部分(评论之前):

我认为你的问题中可能缺少某些内容。

当我将你的播放器变量放入JSfiddle然后执行

document.getElementById("maxhealth").innerHTML = player.maxhealth();

一切正常:

http://jsfiddle.net/630jv67p/