js报错- cannot set property xxx of undefined
生活随笔
收集整理的這篇文章主要介紹了
js报错- cannot set property xxx of undefined
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
case1
報錯代碼
quizList[index] = currentQuiz; quizList[index].learningItem = item;報錯內(nèi)容
Cannot read properties of undefined (reading 'learningItem')報錯原因
currentQuiz對象有可能是undefined,如果再給undefined對象quizList[index]動態(tài)添加屬性learningItem,會導(dǎo)致出現(xiàn)cannot set property xxx of undefined報錯
解決方法
quizList[index] = currentQuiz == undefined ? {} : currentQuiz; quizList[index].learningItem = item;判斷一下quizList[index]是否是undefined即可
case2
報錯代碼
quiz.finishedstudent.forEach(item => {if (item.score < 80) {errorNum++;} })報錯內(nèi)容
Cannot read properties of undefined (reading 'forEach')解決方法
if (quiz.finishedstudent != undefined) {quiz.finishedstudent.forEach(item => {if (item.score < 80) {errorNum++;}}) }總結(jié)
以上是生活随笔為你收集整理的js报错- cannot set property xxx of undefined的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智慧校园建设分享
- 下一篇: 华为OD机试题:密码截取