我在我的Ionic应用程序中使用AdMob插件,如文档所示,一种测试插件是否正确加载的方法是执行以下操作:
if(AdMob) {
///other config code...
}
现在,在设备上,这个功能运行得非常好。但是,在浏览器中它不起作用; 它会在控制台日志中抛出一个错误:AdMob未定义
。
我已经找到了一种解决方法来测试插件的存在(而不会在控制台中抛出错误):
if (window.AdMob){...}
我在StackOverflow上看到了多个问题都使用了这种用法。然而,我无法找到一个解释为什么这不会抛出错误的说明。
我有一些模糊的想法,但是如果有经验丰富的人能够更详细地解释一下,我将不胜感激。
编辑:我进行了如下的额外测试:
var a = "hi";
console.log(a); //shows "hi"
console.log(b); //throws an error that b is not defined
var c = {};
c.b = "hi again";
console.log(c.b); //shows "hi again" as expected
//and now for the grand finale
console.log(c.something);//doesn't throw an error, please explain to me in more detail why?