【发布时间】:2016-09-17 15:40:22
【问题描述】:
我想知道当我将Object.defineProperty() 方法与get() 和set() 一起使用时,为什么在catch 块内没有引发错误?
try {
var f;
Object.defineProperty(window, 'a', {
get: function() {
return fxxxxx; // here: undef var but no error catched
},
set: function(v) {
f = v;
}
});
} catch (e) {
console.log('try...catch OK: ', e);
}
a = function() {
return true;
}
window.a();
// Expected output: "try...catch OK: ReferenceError: fxxxxx is not defined"
// Console output: "ReferenceError: fxxxxx is not defined"
【问题讨论】:
-
谢谢你,克劳德。很好的答案。
-
不过,在未来,最好使用 Stack Snippets(
<>工具栏按钮)来创建可运行的现场演示,而不是使用非现场的 jsFiddle。 (我的答案有一个 sn-p,所以你可以看到它们是什么样的。) -
我下次再做。谢谢。
标签: javascript try-catch getter-setter defineproperty