您好,我试着做了一些计算如下。
权重值应该从分数中减去它的值。但是当我这样做时,我得到了“未定义”作为 score
变量输出。是什么原因?我是否正确处理了变量声明?
var score = 10;
var scoresFunc = function (weight) {
console.log(score);
var finalScore = score - weight;
score = score - finalScore;
var scoreForStartingDate = $('#scoreForStartingDate').val();
var scoreForDuration = $('#scoreForDuration').val();
var scoreForProjects = $('#scoreForProjects').val();
var score = score - parseInt(weight);
var a = '';
for (i = 1; scores >= i; i++) {
a += "<option value='" + i + "'>" + i + "</option>";
}
return a;
};
最佳答案
您看到 console.log(score) 未定义记录的原因是因为名为 variable hoistingMDN 的东西。
var score = 10;
var scoresFunc = function (weight) {
console.log(score);
var score = score - weight;
};
这是所示函数的精简版。然而,吊装的结果,其实是这样的
var score = 10;
var scoresFunc = function (weight) {
var score;
console.log(score);
score = score - weight;
};
这应该可以澄清为什么您记录 undefined
。
关于javascript - 变量在 JavaScript 本地函数中时没有获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086950/