Javascript 如何检查JS中是否存在全局函数?
我得到了一个全局函数:Javascript 如何检查JS中是否存在全局函数?,javascript,Javascript,我得到了一个全局函数: GlobalFunctions = { something: function() { } }; 我知道如何检查函数是否存在: if (typeof functionName == "function") 或者更好: if (typeof functionName === "function") 但是,当我试图确定全局函数是否有效时,上面提到的那些仍然返回下一个错误: if (typeof GlobalFunctions.something
GlobalFunctions = {
something: function() {
}
};
我知道如何检查函数是否存在:
if (typeof functionName == "function")
或者更好:
if (typeof functionName === "function")
但是,当我试图确定全局函数是否有效时,上面提到的那些仍然返回下一个错误:
if (typeof GlobalFunctions.something == "function")
给出了:
angular.js:12520 ReferenceError: GlobalFunctions is not defined
at r.$scope.continueLogout (my-app.js:197)
at b.$scope.logout (my-app.js:243)
at fn (eval at compile (angular.js:13365), <anonymous>:4:209)
at e (angular.js:23613)
at b.$eval (angular.js:16052)
at b.$apply (angular.js:16152)
at HTMLAnchorElement.<anonymous> (angular.js:23618)
at HTMLAnchorElement.dispatch (jquery.min.js:3)
at HTMLAnchorElement.q.handle (jquery.min.js:3)
angular.js:12520引用错误:未定义全局函数
r.$scope.continueLogout(我的应用程序js:197)
在b.$scope.logout(我的app.js:243)
在fn(编译时求值(angular.js:13365),:4:209)
在e(angular.js:23613)
以b.$eval计算(angular.js:16052)
在b.$apply(angular.js:16152)
在兰开夏。(angular.js:23618)
在htmlanchorement.dispatch(jquery.min.js:3)
位于htmlanchorement.q.handle(jquery.min.js:3)
我在谷歌上搜索过,但只找到了函数的解决方案,而没有找到全局函数的解决方案
希望这足够清楚,谢谢。您在声明对象时没有使用
var
/let
/const
关键字。我猜你的代码是用 如果不启用
严格模式
,则可以在不使用var
关键字的情况下声明变量,这将在全局对象上设置变量:
首先,严格模式使得不可能意外地创建全局
变量。在普通JavaScript中,在赋值中错误键入变量
在全局对象上创建新属性并继续“工作”
在严格模式下
:
“严格使用”
全局函数={
某物:函数(){
}
};
if(GlobalFunctions)console.log('yes')代码>找到了答案:
if (typeof GlobalFunctions != "undefined")
我真的不明白为什么要投否决票。显示你的代码。这是代码…不,不是。这个错误来自angular.js,我假设它不是测试的地方typeof x
永远不会抛出“未定义”错误。嗯,typeof functionName
与typeof GlobalFunctions非常不同。有些东西
。