这更像是一个概念性的问题,但具体来说,我正在(当前)编写一个包含大量交付代码的 javascript 文件(即供应商支持他们交付的代码,而不是我们的定制)。
因为交付的代码始终执行是必不可少的,所以我始终封装我的自定义代码的方法try{} catch(e){}
是否确保即使我的自定义代码失败,交付的代码也会执行?
我封装了每一点自定义代码。即使我要使用交付的变量,我也会将其分配给自定义 var,甚至将一行分配封装在try{} catch(e){}
.
这种方法有效吗?这种方法是否矫枉过正和/或如何改进?
这是我目前所做的:
//delivered code
var global1 = true;
var global2 = true;
//my custom code
try{
var custom_global3 = true;
} catch(e){
console.error(e);
}
//more delivered code
if(global1)
doSomethingAwesome(global2);
//my custom code
try{
makeItLessAwesome(custom_global3);
} catch(e){
console.error(e);
}