我想知道为什么错误不会在catch块内部引发,当我使用Object.defineProperty()
方法时get()
和set()
?JavaScript try ... catch for defineProperty无法正常工作
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"
谢谢你,克劳德。非常好的答案。 – poro6
尽管如此,最好使用Stack Snippets('<>'工具栏按钮)来创建可运行的现场演示,而不是jsFiddle,这是不在现场的。 (我的答案有一个片段,所以你可以看到他们是什么样的。) –
我会在下次做。谢谢。 – poro6