Javascript Uncaught TypeError:无法读取未定义的属性“0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29431225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Uncaught TypeError: Cannot read property '0' of undefined
提问by Greg Uptron
I know there's plenty of questions related to this error and I've checked most of them and none help me solve my issue. (Which seems so easy to debug...)
我知道有很多与这个错误相关的问题,我已经检查了其中的大部分,但没有一个能帮助我解决我的问题。(这似乎很容易调试......)
I have an array (which is empty aat first):
我有一个数组(首先是空的):
var words = [];
And my function hasLetter, checks if we find a letter (object) in the array (that I call here: d) words.
我的函数 hasLetter,检查我们是否在数组中找到了一个字母(对象)(我在这里称之为:d)词。
function hasLetter(letter,d){
// if words[0] not null should return object of letter "a", here we getting
// the index of the letter (since ascii of "a" is 97, I substract 97)
var ascii = letter.charCodeAt(0)-97;
//Trying to not get an error with this but still creates an err
if(typeof d[ascii ] !== "undefined" && d[ascii ] !== null && d[ascii ].length > 0){
if(d[ascii].letter == letter){
return true;
}
}
return false; }
and I have a function called addLetter which checks if hasLetter returns true/false and then creates or not accordingly a new node.
我有一个名为 addLetter 的函数,它检查 hasLetter 是否返回 true/false,然后相应地创建或不创建一个新节点。
function addLetter(letter,d){
var ascii = letter.charCodeAt(0)-97;
if(!hasLetter(letter,d)){
document.write("This letter" + letter + " hasn't been found in words.");
d[ascii] = new Node(letter);
}
document.write("This letter " + letter + " already exists in words.");
document.write(d[ascii].letter);
}
}
and if I test:
如果我测试:
addLetter("a",words);
it returns:
它返回:
Uncaught TypeError: Cannot read property '0' of undefined
I don't know what to do to say "if it's undefined then don't look into it or something along those lines...
我不知道该怎么说“如果它是未定义的,那么就不要研究它或类似的东西......
Thanks
谢谢
回答by theonlygusti
The error is here:
错误在这里:
hasLetter("a",words[]);
You are passing the first item of words, instead of the array.
您正在传递words, 而不是数组的第一项。
Instead, pass the array to the function:
相反,将数组传递给函数:
hasLetter("a",words);
Problem solved!
问题解决了!
Here's a breakdown of what the problem was:
以下是问题所在的细分:
I'm guessing in your browser (chrome throws a different error), words[] == words[0], so when you call hasLetter("a",words[]);, you are actually calling hasLetter("a",words[0]);. So, in essence, you are passing the first item of words to your function, notthe array as a whole.
我猜测在您的浏览器中(chrome 会引发不同的错误)words[] == words[0],因此当您调用 时hasLetter("a",words[]);,实际上是在调用hasLetter("a",words[0]);. 因此,本质上,您将单词的第一项传递给您的函数,而不是整个数组。
Of course, because wordsis just an empty array, words[0]is undefined. Therefore, your function call is actually:
当然,因为words只是一个空数组,words[0]is undefined。因此,您的函数调用实际上是:
hasLetter("a", undefined);
which means that, when you try to access d[ascii], you are actually trying to access undefined[0], hence the error.
这意味着,当您尝试访问时d[ascii],您实际上是在尝试访问undefined[0],因此会出现错误。
回答by Akash Chavda
There is no error when I use your code,
当我使用你的代码时没有错误,
but I am calling the hasLettermethod like this:
但我正在调用这样的hasLetter方法:
hasLetter("a",words);

