List of global user defined functions in JavaScript?

后端 未结 3 1974
南方客
南方客 2020-11-30 06:28

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:

<         


        
3条回答
  •  一向
    一向 (楼主)
    2020-11-30 06:54

    As Chetan Sastry suggested in his answer, you can check for the existance of [native code] inside the stringified function:

    Object.keys(window).filter(function(x)
    {
        if (!(window[x] instanceof Function)) return false;
        return !/\[native code\]/.test(window[x].toString()) ? true : false;
    });
    

    Or simply:

    Object.keys(window).filter(function(x)
    {
        return window[x] instanceof Function && !/\[native code\]/.test(window[x].toString());
    });
    

    in chrome you can get all non-native variables and functions by:

    Object.keys(window);
    

提交回复
热议问题