2017-08-08 61 views
0

我在为小型网站编写脚本时遇到了或多或少的大问题。如何避免在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,所以如果这是避免这种混乱并使事情变得简单的唯一可能的方式,请告诉我。

+0

为什么有些点成本负运行bonusPointsCosts? – clabe45

+0

旁注:你似乎在if语句中分配。那是故意的吗? – k0pernikus

+0

如果您发现您的代码过于重复,请制作一个执行重复任务的功能,并使用参数更改所做的事情。 – 4castle

回答

0

只是东西洗衣清单,可以帮助:

- 一个简单的解决方案,以清理变量是使用对象文本。这只是一个JSON格式的对象,它是一个变量。

// Object literal 
var stats = { 
    "Endurance" : 4, 
    "Strength" : 4, 
    "Agility" : 4, 
    "Magic" : 4, 
    "Mind" : 4, 
    "Charisma" : 4, 
    "BonusPoints" : 20 
}; 

// Calling the object 
stats.Endurance; // 4 

你可以清理所有的变量。

更高级的选项可能是使用构造函数来创建stat的每个实例。 ES5版本:

// Creation of `stat` object requires a model name passed into it 
function stat(endurance, strength, agility, magic, mind, points, bonus) { 
    this.endurance = endurance; 
    this.strength = strength; 
    this.agility = agility; 
    this.magic = magic; 
    this.charisma = charisma; 

    // Flattens the whole thing out - but you could do whatever here 
    this.toString = function() { 
    return this.endurance; 
    } 
} 

// Create list of stats to create 
var newStatObject = new stat(4,4,4,4,4,4,0,20); 

- 你switch声明只有一个功能,所以非常难以减少任何进一步。我没有看到任何jQuery,所以你很好。

您已经列在最后

-The一系列功能将与单一功能+ switch声明你上面使用模式得到更好的服务(以自由,以不改写一切,但你应该明白我的意思):

function MAGCheck(magVar) { 
    switch { 
    case 1: 
    if (magVar >= 2) { 
     displayStats(); 
    } 
    break; 
    case 2: 
    if (magVar <= 9) { 
     MND++; 
     pointCosts(MND); 
     displayStats(); 
    } 
    break; 
    } 
} 

- 同样,注意你的底,你的比较将无法正常工作的功能列表,因为你正在使用的不是比较运算符分配(=)(== | ===):

function minusEND() { 
    if (END >= 2) { 
    if (enoughpoints === true) { 
     END--; 
     pointCosts(END); 
     displayStats(); 
     plusgrayitout(); 
    } 
    } 
    document.getElementById('plusEND').removeAttribute("onclick"); 
} 
0

这就是我已经做到了
牛逼

var stats = { 
    "Endurance": 4, 
    "Strength": 4, 
    "Agility": 4, 
    "Magic": 4, 
    "Mind": 4, 
    "Charisma": 4, 
    "BonusPoints": 20 
}; 

var formular = { 
    "f1": +3, 
    "f2": +2, 
    "f3": +1, 
    "f4": -0, 
    "f5": -1, 
    "f6": -3, 
    "f7": -5, 
    "f8": -8, 
    "f9": -11, 
    "f10": -15 
}; 

var submitSwitch = false; 


function checkBonusPointsFromBeingInvalid() { 
    if (stats.bonusPoints <= 0) { 
    submitSwitch = false; 
    } 
} 

function displayStats() { 
    document.getElementById("endurance").value = stats.Endurance; 
    document.getElementById("strength").value = stats.Strength; 
    document.getElementById("agilility").value = stats.Agility; 
    document.getElementById("magic").value = stats.Magic; 
    document.getElementById("mind").value = stats.Mnd; 
    document.getElementById("charisma").value = stats.Charisma; 
    document.getElementById("points").innerHTML = "Bonus Points " + stats.BonusPoints; 
    console.log("Displayed Stats!"); 
} 

function bonusPointsCosts(args) { 

    switch (args) { 
    case 1: 
     if (args === 1) { 
     bonusPointsToReduce = bonusPointsToReduce + formular.f1; 
     } 
     break; 
    case 2: 
     if (args === 2) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f2; 
     } 
     break; 
    case 3: 
     if (args === 3) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f3; 
     } 
     break; 
    case 4: 
     if (args === 4) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f4; 
     } 
     break; 
    case 5: 
     if (args === 5) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f5; 
     } 
     break; 
    case 6: 
     if (args === 6) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f6; 
     } 
     break; 
    case 7: 
     if (args === 7) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f7; 
     } 
     break; 
    case 8: 
     if (args === 8) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f8; 
     } 
     break; 
    case 9: 
     if (args === 9) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f9; 
     } 
     break; 
    case 10: 
     if (args === 10) { 
     stats.BonusPoints = 20; 
     stats.BonusPoints = stats.BonusPoints + formular.f10; 
     } 
     break; 
    } 
} 
////////////TEST////////////////////////////////////// 
stats.Endurance = 4; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " +stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 5; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 6; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 7; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 6; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 5; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 4; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 
stats.Endurance = 3; 
bonusPointsCosts(stats.Endurance); 
console.log("Endurance: " + stats.Endurance + "\n" + "Bonus Points: " + stats.BonusPoints + "\n"); 

我的问题是每一个统计throught