没有 try-catch
,下面的代码片段抛出 Uncaught ReferenceError: myObj is not defined
try {
if (myObj !== null && typeof myObj !== "undefined");
} catch (e) {
document.getElementById('error').innerHTML = e.Message;
}
<p>The error is
<mark id='error'></mark>
<p>
为什么错误消息没有打印在 <mark id='error'></mark>
内?
最佳答案
只需从异常对象中删除 Message 属性,只需 e 本身就足够了。
document.getElementById('error').innerHTML = e;
这会起作用
关于javascript - 为什么在这种情况下不会抛出 "object is not defined"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34391254/