我想總結兩個功能,它們是同一個對象的一部分。如何在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
};
好了,你可以看到,我會爲每個統計,基地/武器/防具/附件/共5個對象屬性。總計是對所有這些統計數據進行總結的函數。
之後,我們有玩家最大健康,也是功能,這是使用「總」統計計算的功能,但因爲它也是一個功能,我不能使它的工作。所以我想出的唯一辦法,是不是 (player.totalEndurance * 10)
我要做((player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance) * 10)
和部分在那裏我的HTML顯示它:
我加(),因爲它的一個函數,但它不工作我得到NaN結果,函數要麼是未定義的,要麼或「對象不是函數」錯誤。
我希望你能幫助我:)謝謝
似乎正常工作對我來說:http://jsfiddle.net/ cwecuxhu/ – 2015-02-06 07:34:24