JavaScript中全局用户定义的函数列表? [英] List of global user defined functions in JavaScript?

查看:115
本文介绍了JavaScript中全局用户定义的函数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在JavaScript中获得用户定义的函数列表?

Is it possible to get a list of the user defined functions in JavaScript?

我目前正在使用它,但它返回的函数不是用户定义的:

I'm currently using this, but it returns functions which aren't user defined:

var functionNames = [];

for (var f in window) {
    if (window.hasOwnProperty(f) && typeof window[f] === 'function') {
        functionNames.push(f);
    }
}


推荐答案

I假设你想过滤掉原生函数。在Firefox中, Function.toString()返回函数体,对于原生函数,它将采用以下形式:

I'm assuming you want to filter out native functions. In Firefox, Function.toString() returns the function body, which for native functions, will be in the form:

function addEventListener() { 
    [native code] 
}

您可以匹配循环中的模式 / \ [native code \] / ,并省略匹配的函数。

You could match the pattern /\[native code\]/ in your loop and omit the functions that match.

这篇关于JavaScript中全局用户定义的函数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆