如何打印在jQuery中手动创建的函数或类?

时间:2013-02-11 18:31:32

标签: jquery

我想打印出我在特定源文件中创建的函数和类。

例如,如果我们假设;

我的函数名称是print()

print_func(script.js)这必须打印 script.js

中的函数

print_class(xyz.js)这必须打印出 xyz.js

中的类

有没有办法呢?

2 个答案:

答案 0 :(得分:0)

您必须检查窗口对象的所有属性,然后在运行脚本后再次检查所有属性。

例如,在运行脚本之前,您将获得所有存在的函数:

before=[];for (a in window) {if (typeof(window[a])=='function') before.push(a)   }

答案 1 :(得分:0)

不,但如果将功能分配给对象,则可以打印出所有可用的功能。正确的命名空间将帮助您轻松实现这一目标。

请参阅Is there a way to print all methods of an object in javascript?

function getMethods(obj) {
  var result = [];
  for (var id in obj) {
    try {
      if (typeof(obj[id]) == "function") {
        result.push(id + ": " + obj[id].toString());
      }
    } catch (err) {
      result.push(id + ": inaccessible");
    }
  }
  return result;
}

使用它:

alert(getMethods(document).join("\n"));