我在为小型网站编写脚本时遇到了或多或少的大问题。如何避免在javasccript中为许多变量生成函数?
我想要做什么是一个统计表创建者,现在只有统计计算。
比方说
var statEndurance = 4;
var statStrength = 4;
var statAgility = 4;
var statMagic = 4;
var statMind = 4;
var statCharisma = 4;
var bonusPoints = 20;
声明的变量。 每个属性只能有1到10个(没问题)难度是自定义点的计算!
Score ____________ Points Cost
_ 1 __________________-3
_ 2 __________________-2
_ 3 __________________-1
_ 4 __________________ 0
_ 5 __________________ 1
_ 6 __________________ 3
_ 7 __________________ 5
_ 8 __________________ 8
_ 9 __________________11
_10 _________________ 15
如果你有5个在一个统计,你需要3个点,以增加其在6
我有什么累。
function pointCosts(args) {
switch (args) {
case 1:
points = points + 3;
displayStats();
break;
case 2:
points = points + 2;
displayStats();
break;
case 3:
points = points + 1;
displayStats();
break;
case 4:
points = points + 1;
displayStats();
break;
case 5:
points = points - 1;
displayStats();
break;
case 6:
points = points - 3;
displayStats();
break;
case 7:
points = points - 5;
displayStats();
break;
case 8:
points = points - 8;
displayStats();
break;
case 9:
points = points - 11;
displayStats();
break;
case 10:
points = points - 15;
displayStats();
break;
}
};
如果我增加“心”到5奖励积分将下降到19 但是如果我改回想起4再次点不会改变,看到上面的代码。
但对于检查每个统计我用
var enoughpoints = true;
function pluscheckpoints(args) {
if (args == 5 && points < 1) {
enoughpoints = false;
}
if (args = 6 && points < 3) {
enoughpoints = false;
}
if (args = 7 && points < 5) {
enoughpoints = false;
}
if (args = 8 && points < 8) {
enoughpoints = false;
}
if (args = 9 && points < 11) {
enoughpoints = false;
}
if (args = 10 && points < 15) {
enoughpoints = false;
}
}
而且
function plusEND() {
pluscheckpoints(plusEND);
if (END <= 9) {
if (enoughpoints = true) {
END++;
pointCosts(END);
displayStats();
minusgrayitout();
}
}
document.getElementById('minusEND').removeAttribute("onclick");
}
function minusEND() {
if (END >= 2) {
if (enoughpoints = true) {
END--;
pointCosts(END);
displayStats();
plusgrayitout();
}
}
document.getElementById('plusEND').removeAttribute("onclick");
}
function plusSTR() {
if (STR <= 9) {
if (enoughpoints = true) {
STR++;
pointCosts(STR);
displayStats();
}
}
}
function minusSTR() {
if (STR >= 2) {
if (enoughpoints = true) {
STR--;
pointCosts(STR);
displayStats();
}
}
}
function plusAGI() {
if (AGI <= 9) {
if (enoughpoints = true) {
AGI++;
pointCosts(AGI);
displayStats();
}
}
}
function minusAGI() {
if (AGI >= 2) {
if (enoughpoints = true) {
AGI--;
pointCosts(AGI);
displayStats();
}
}
}
function plusMAG() {
if (MAG <= 9) {
if (enoughpoints = true) {
MAG++;
pointCosts(MAG);
displayStats();
}
}
}
function minusMAG() {
if (MAG >= 2) {
if (enoughpoints = true) {
MAG--;
pointCosts(MAG);
displayStats();
}
}
}
function plusMND() {
if (MND <= 9) {
if (enoughpoints = true) {
MND++;
pointCosts(MND);
displayStats();
}
}
}
function minusMND() {
if (MND >= 2) {
if (enoughpoints = true) {
MND--;
pointCosts(MND);
displayStats();
}
}
}
function plusCHA() {
if (CHA <= 9) {
if (enoughpoints = true) {
CHA++;
pointCosts(CHA);
displayStats();
}
}
}
function minusCHA() {
if (CHA >= 2) {
if (enoughpoints = true) {
CHA--;
pointCosts(CHA);
displayStats();
}
}
}
你有没有办法避免的许多功能?我不喜欢jquery,所以如果这是避免这种混乱并使事情变得简单的唯一可能的方式,请告诉我。
为什么有些点成本负运行bonusPointsCosts? – clabe45
旁注:你似乎在if语句中分配。那是故意的吗? – k0pernikus
如果您发现您的代码过于重复,请制作一个执行重复任务的功能,并使用参数更改所做的事情。 – 4castle