[英]Uncaught TypeError cannot read property 'name' of undefined
I'm new to Javascript/jquery and having an issue with an app I'm developing. 我是Javascript / jquery的新手,我正在开发一个应用程序的问题。 I am getting an Uncaught TypeError when trying to read a property of an object.
我在尝试读取对象的属性时遇到未捕获的TypeError。 My ultimate goal is for the user to enter something in a text box and based on that input, make an object with the input as a name property.
我的最终目标是让用户在文本框中输入内容并根据该输入创建一个带有输入的对象作为名称属性。 Here is my code so far:
到目前为止,这是我的代码:
$(document).ready(function(){
var playerArray = [];
var playerIndex = 0;
function player (name) {
this.name = name;
score = 0;
};
var addPlayer = function(name){
playerArray[playerIndex] = new player(name);
playerIndex++;
};
$('#add_players').on('click', '#btn-add', function(){
var toAdd = $('input[name=playerNameInput]').val();
addPlayer(toAdd);
$('#playerList').append('<div class="ui-block-a" style="padding:1em">' + playerArray[playerIndex].name + '</div>');
});
});
I have searched the site for other issues like this but they are all dealing with APIs or irrelevant issues to mine. 我在网站上搜索过这样的其他问题,但他们都处理API或我的无关问题。 I would greatly appreciate any help.
我非常感谢任何帮助。
Look at this code 看看这段代码
var addPlayer = function(name){
playerArray[playerIndex] = new player(name); //you store it at an index
playerIndex++; //you increment
};
Because index is incremented and there is nothing like the position you are reading. 因为索引会增加,并且没有像您正在阅读的位置那样。
console.log(playerArray[playerIndex]); //undefined
console.log(playerArray[playerIndex-1]); //the last entry you added
Personally I would not rely on the last index. 我个人不会依赖最后一个索引。 I would return the new player when you create it.
我会在你创建它时返回新玩家。 Than there is no need to deal with index issues.
比没有必要处理索引问题。
var playerArray = [];
var playerIndex = 0;
function Player (name) {
this.name = name;
score = 0; //this will be a problem....
};
var addPlayer = function(name){
var user = new Player(name);
playerArray.push(user);
return user;
};
$('#add_players').on('click', '#btn-add', function(){
var toAdd = $('input[name=playerNameInput]').val();
var person = addPlayer(toAdd);
$('#playerList').append('<div class="ui-block-a" style="padding:1em">' + person.name + '</div>');
});
Also score
is going to be a problem. score
也将是一个问题。
Your first run is adding 1 to the playerIndex
that you initialize at 0. 您的第一次运行是将1初始化为0的
playerIndex
添加1。
By the time you get to playerArray[playerIndex].name
for the first time, playerIndex
is 1 and you are looking for the first value (index 0). 当你第一次进入
playerArray[playerIndex].name
时, playerIndex
为1,你正在寻找第一个值(索引0)。 You will always be one index behind. 你永远是一个落后的指数。
addPlayer()
is incrementing your playerIndex
counter after you've added the object to playerArray
, which you then use inside your click
handler. addPlayer()
是递增的playerIndex
计数器您已经添加了对象后 playerArray
,你那么你的内部使用click
处理程序。 It's not clear based on your code what you're trying to retrieve when you call playerArray[playerIndex]
(the last element added?) but I would just get rid of the counter and use Array.prototype.push()
instead: 当你调用
playerArray[playerIndex]
(添加的最后一个元素?)时,根据你的代码你要检索的内容并不清楚,但我只是摆脱了计数器并使用Array.prototype.push()
代替:
$(document).ready(function(){
var playerArray = [];
function player (name) {
this.name = name;
score = 0;
}
var addPlayer = function(name){
playerArray.push(new player(name));
};
$('#add_players').on('click', '#btn-add', function(){
var toAdd = $('input[name=playerNameInput]').val();
addPlayer(toAdd);
// playerArray[playerArray.length - 1] will always retrieve the last element in the array
// use playerArray[0] if you always want the first
$('#playerList').append('<div class="ui-block-a" style="padding:1em">' + playerArray[playerArray.length - 1].name + '</div>');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.