我正在为SkillCrush开发一个项目,并且遇到“未定义”错误,感觉好像我已在代码顶部定义了变量。我100%知道我做错了事,但不确定是什么。有什么建议吗?
var createPolitician = function (name)
{
var politician = {}; //is this not defined here?
politician.name = name;
politician.electionResults = null;
politician.totalVotes = 0;
return politician;
};
var oscar = createPolitician("Oscar Jiminez");
var luke = createPolitician("Luke Spencer");
oscar.electionResults = [5, 1, 7, 2, 33, 6, 4, 2, 1, 14, 8, 3, 1, 11, 11, 0, 5, 3, 3, 3, 7, 4, 8, 9, 3, 7, 2, 2, 4, 2, 8, 3, 15, 15, 2, 12, 0, 4, 13, 1, 3, 2, 8, 21, 3, 2, 11, 1, 3, 7, 2];
luke.electionResults = [4, 2, 4, 4, 22, 3, 3, 1, 2, 15, 8, 1, 3, 9, 0, 6, 1, 5, 5, 1, 3, 7, 8, 1, 3, 3, 1, 3, 2, 2, 6, 2, 14, 0, 1, 6, 7, 3, 7, 3, 6, 1, 3, 17, 3, 1, 2, 11, 2, 3, 1];
oscar.electionResults[9] = 1;
luke.electionResults[9] = 28;
oscar.electionResults[4] = 17;
luke.electionResults[4] = 38;
oscar.electionResults[43] = 11;
luke.electionResults[43] = 27;
console.log(oscar.electionResults);
console.log(luke.electionResults);
politician.countTotalVotes = function()
{
this.totalVotes = 0;
for (var i = 0; i < this.electionResults.length; i++);
{
this.totalVotes = this.totalVotes + this.electionResults[i];
}
}
oscar.countTotalVotes();
luke.countTotalVotes();
console.log(oscar.totalVotes);
console.log(luke.totalVotes);
错误:
"error"
"ReferenceError: politician is not defined
at reloyur.js:32:1"
答案 0 :(得分:1)
将countTotalVotes函数移至创建函数:
var createPolitician = function (name)
{
var politician = {};
politician.name = name;
// set to an empty array so this is defined
politician.electionResults = [];
politician.totalVotes = 0;
// here politician exists, add a function to the object that you can call later
politician.countTotalVotes = function()
{
this.totalVotes = 0;
for (var i = 0; i < this.electionResults.length; i++)
{
this.totalVotes = this.totalVotes + this.electionResults[i];
}
}
return politician;
};
答案 1 :(得分:1)
politician.countTotalVotes = ...
这不会起作用,因为politician
仅在创建luke
或oscar
时存在(然后指向其中之一)。相反,您可以这样做:
luke.countTotalVotes = oscar.countTotalVotes = function() { /*...* };
但是对于更多的政治家来说,情况变得有些复杂。但是,您可以将政治家传递给以下人员:
function countTotalVotes(politician) {
//...
}
countTotalVotes(luke);
或者您使用继承的力量。
答案 2 :(得分:1)
正如我在评论中建议的那样,您可以将Politician
重写为class
,这基本上为您提供了可重用功能的模板。如果您不熟悉此概念,则一般来说,"prototypal inheritance"是跟进该主题的好词。现在,这是使用class
重写脚本的方法:
class Politician {
constructor (name) {
this.name = name;
this.electionResults = [];
}
// no need to even write a .countTotalVotes() method
// just define .totalVotes as a getter that does the work
get totalVotes () {
var total = 0;
for (var i = 0; i < this.electionResults.length; i++) {
total += this.electionResults[i];
}
return total;
}
}
var oscar = new Politician("Oscar Jiminez");
var luke = new Politician("Luke Spencer");
oscar.electionResults = [5, 1, 7, 2, 33, 6, 4, 2, 1, 14, 8, 3, 1, 11, 11, 0, 5, 3, 3, 3, 7, 4, 8, 9, 3, 7, 2, 2, 4, 2, 8, 3, 15, 15, 2, 12, 0, 4, 13, 1, 3, 2, 8, 21, 3, 2, 11, 1, 3, 7, 2];
luke.electionResults = [4, 2, 4, 4, 22, 3, 3, 1, 2, 15, 8, 1, 3, 9, 0, 6, 1, 5, 5, 1, 3, 7, 8, 1, 3, 3, 1, 3, 2, 2, 6, 2, 14, 0, 1, 6, 7, 3, 7, 3, 6, 1, 3, 17, 3, 1, 2, 11, 2, 3, 1];
oscar.electionResults[9] = 1;
luke.electionResults[9] = 28;
oscar.electionResults[4] = 17;
luke.electionResults[4] = 38;
oscar.electionResults[43] = 11;
luke.electionResults[43] = 27;
// console.log(oscar.electionResults);
// console.log(luke.electionResults);
// these are no longer required
// oscar.countTotalVotes();
// luke.countTotalVotes();
console.log(oscar.name, 'has', oscar.totalVotes, 'votes');
console.log(luke.name, 'has', luke.totalVotes, 'votes');
答案 3 :(得分:0)
错误消息应随附行号和列号。在这种情况下,错误发生在第32行:
politician.countTotalVotes = function()
在createPolitician
函数的定义中,您声明了一个名为politician
的变量。 var
创建一个变量,该变量驻留在声明该函数的函数中。如果它不在函数内部,则创建一个全局变量。
您保留了在其中声明变量的函数,因此该函数不再可用。
如果考虑一下,以后再使用该politician
变量将非常模棱两可。它指的是什么?
如果您希望能够致电给政治人物countTotalVotes()
,那么帕特里克(Patrick)有关使用班级的评论是一个不错的选择。您还可以将函数分配给从politician
返回的createPolitician
对象-这将为政客的每个实例提供其自己的函数副本。
答案 4 :(得分:0)
我更正了您在代码中的错误。您可以像下面这样更改代码:
function createPolitician(name)
{
var politician =
{
name: name,
electionResults: null,
totalVotes: 0,
countTotalVotes: function()
{
//this.totalVotes = 0; <- irrelevant because you use it only one time
for(var i = 0; i < this.electionResults.length; i++)//";" symbol on the end is your mistake too
{
this.totalVotes += this.electionResults[i];
}
}
};
return politician;
};
var oscar = createPolitician("Oscar Jiminez");
var luke = createPolitician("Luke Spencer");
oscar.electionResults = [5, 1, 7, 2, 33, 6, 4, 2, 1, 14, 8, 3, 1, 11, 11, 0, 5, 3, 3, 3, 7, 4, 8, 9, 3, 7, 2, 2, 4, 2, 8, 3, 15, 15, 2, 12, 0, 4, 13, 1, 3, 2, 8, 21, 3, 2, 11, 1, 3, 7, 2];
luke.electionResults = [4, 2, 4, 4, 22, 3, 3, 1, 2, 15, 8, 1, 3, 9, 0, 6, 1, 5, 5, 1, 3, 7, 8, 1, 3, 3, 1, 3, 2, 2, 6, 2, 14, 0, 1, 6, 7, 3, 7, 3, 6, 1, 3, 17, 3, 1, 2, 11, 2, 3, 1];
oscar.electionResults[9] = 1;
luke.electionResults[9] = 28;
oscar.electionResults[4] = 17;
luke.electionResults[4] = 38;
oscar.electionResults[43] = 11;
luke.electionResults[43] = 27;
console.log(oscar.electionResults);
console.log(luke.electionResults);
oscar.countTotalVotes();
luke.countTotalVotes();
console.log(oscar.name, 'has', oscar.totalVotes, 'votes');
console.log(luke.name, 'has', luke.totalVotes, 'votes');
答案 5 :(得分:0)
这与variable scope有关。 在createPolitician函数之外声明政治家对象变量 ,以便您访问该函数。无法访问其范围,这就是为什么您会得到未定义的错误的原因。