Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查javascript函数是否存在或已定义_Javascript_Function - Fatal编程技术网

检查javascript函数是否存在或已定义

检查javascript函数是否存在或已定义,javascript,function,Javascript,Function,在调用函数之前,我想知道函数是否存在或是否已定义 我有一行代码: function renderView(){ // some code } 您可以检查typeof,如果未定义或超出范围,则返回undefined;如果是函数,则返回function if ( typeof renderView == 'function' ) { // it exists } 感谢您使用typeof作为运算符,而不是像所有其他傻瓜一样的函数:)//简单的函数,可以判断函数是否已定义函数is_func

在调用函数之前,我想知道函数是否存在或是否已定义

我有一行代码:

function renderView(){
 // some code
}

您可以检查typeof,如果未定义或超出范围,则返回
undefined
;如果是函数,则返回
function

if ( typeof renderView == 'function' ) {
    // it exists
}

感谢您使用typeof作为运算符,而不是像所有其他傻瓜一样的函数:)
//简单的函数,可以判断函数是否已定义函数is_function(func){return typeof window[func]!='undefined'&&$.isFunction(window[func]);}//用法if(is_function(“myFunction”){alert(“myFunction defined”);}其他{alert(“myFunction not defined”);}
使用jQuery并假设函数是在全局范围内定义的?