JavaScript 中的“TypeError: ‘undefined’ is not an object”
在未定義的物件上訪問屬性或呼叫方法時,會發生“TypeError: ‘undefined’ is not an object”錯誤。該錯誤只在 Safari 瀏覽器中顯示。
以下是在 JavaScript 中“TypeError - ‘undefined’ is not an object”的程式碼錯誤 −
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-size: 18px;
font-weight: 500;
color:blueviolet
}
</style>
</head>
<body>
<h1>TypeError: ‘undefined’ is not an object example</h1>
<div class="result"></div>
<br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to access the name property of person object</h3>
<script>
var resEle = document.querySelector('.result');
var BtnEle = document.querySelector('.Btn');
var person;
BtnEle.addEventListener('click',function(){
try{
alert(person.name);
}
catch(err){
resEle.innerHTML=err;
}
});
</script>
</body>
</html>輸出

點選“點這裡”按鈕 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP