2015-02-06 88 views
-1

我想總結兩個功能,它們是同一個對象的一部分。如何在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結果,函數要麼是未定義的,要麼或「對象不是函數」錯誤。

我希望你能幫助我:)謝謝

+0

似乎正常工作對我來說:http://jsfiddle.net/ cwecuxhu/ – 2015-02-06 07:34:24

回答

2

第2部分(最終):

要計算使用totalEndurance需要括號添加到您的電話,因爲它是一個功能。

player.totalEndurance() 

第1部分(前評論):

我認爲有些事情可能會在你的問題中丟失。

當我把您的播放器變量中的jsfiddle,然後執行

​​

一切正常:

http://jsfiddle.net/630jv67p/

+0

因爲這部分其實是'((player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance)* 10); },'我測試過它是否以這種方式工作,但我想使用'player.totalEndurance * 10'而不是 – Mariusz 2015-02-06 08:28:23

+0

totalEndurance不是原始問題的播放器中定義的變量。我看到你添加了它作爲一個函數,所以要訪問它,你需要做'player.totalEndurance()* 10'和其他任何你想引用它,你需要引用它作爲'player.totalEndurance()' – 2015-02-06 08:37:45

+0

哦,是的 - - 謝謝,作出答覆或編輯它,所以我可以再次接受^^再次感謝 – Mariusz 2015-02-06 08:56:07

相關問題