Uncaught TypeError: Cannot read property of undefined In JavaScript


 

當腳本遇到未初始化的變量或對象時,通常會拋出如上圖所示的錯誤。

 

Decription

'Undefined'是全局對象的屬性。如果沒有為變量賦值,則為'undefined'類型。當求值變量沒有任何賦值時,代碼也會返回未定義的值。

 

Code structure

function test(t) {      //defining a function
  if (t === undefined) {       //if t=undefined, call tt
        console.log(t.tt)      //call tt member from t
  }
  return t;    
}
var a;    //a is a variable with undefined value
console.log(test(a)); //function call

 

Error

運行以上代碼后,將會看到一個錯誤提示:

 

Debugging

需要在代碼中判斷是否變量是 undefined

if (typeof(jsvariable) == 'undefined') {
  ...
}

 

https://codeburst.io/uncaught-typeerror-cannot-read-property-of-undefined-in-javascript-c81e00f4a5e3

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM