2015-02-06 5 views
-1

동일한 객체의 일부인 두 개의 함수를 요약하려고합니다.자바 스크립트에서 두 개의 다른 함수를 요약하는 방법

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, base/weapon/armor/accessory/total에 5 가지 개체 속성이 있습니다. 합계는 모든 통계를 요약하는 함수입니다.

그 후 우리는 선수 maxhealth, 피해도 "총"stat를 사용하여 계산되는 기능이지만, 그것도 작동하지 않는 기능도 있기 때문에. 내가 알아 낸 유일한 방법은, 대신 (player.totalEndurance * 10)

의에 있습니다 나는 HTML에 표시 ((player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance) * 10)

과 일부해야 할 보낸 사람

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

내가 (추가)를 그 함수가 작동하지 않지만 NaN 결과를 얻거나 함수가 정의되지 않았거나 "객체가 함수가 아닙니다"오류가 발생합니다.

은 당신이 :) 나를 도와 덕분에

+0

나를 위해 잘 작동하는 것 같다 : http://jsfiddle.net/ 이 부분이 실제로'((player.baseEndurance + player.weaponEndurance + player.armorEndurance + player.accessoryEndurance) * 10)이기 때문에 cwecuxhu/ –

답변

2

2 부 (최종) 할 수 있기를 바랍니다 :

당신이 함수이기 때문에, 당신의 전화에 괄호를 추가 할 필요가 totalEndurance을 사용하여 계산합니다.

player.totalEndurance() 

1 부 (전 주석) :

는 뭔가가 귀하의 질문에 나타나지 않을 수 있습니다 생각합니다. 내가 JSfiddle에 플레이어 변수를 넣고

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

모든 것을 실행

잘 작동 :

http://jsfiddle.net/630jv67p/

+0

; }'이 방법으로 작동하는지 테스트했지만 대신 player.totalEndurance * 10을 사용하려고합니다 – Mariusz

+0

totalEndurance는 원래 질문의 플레이어에 정의 된 변수가 아닙니다. 당신이 그것을 함수로 추가 한 것을 볼 수 있습니다. 그래서 그것을 접근하기 위해서는'player.totalEndurance() * 10' 그리고 다른 곳에서는 그것을 참조하기를 원할 것입니다.'player.totalEndurance()'로 참조해야합니다. –

+0

오, 예 ~ _- 감사합니다. 대답을하거나 편집하여 ^^ 다시 감사합니다. – Mariusz

관련 문제