當腳本遇到未初始化的變量或對象時,通常會拋出如上圖所示的錯誤。
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