我想打印出我在特定源文件中创建的函数和类。
例如,如果我们假设;
我的函数名称是print()
print_func(script.js)
这必须打印 script.js
print_class(xyz.js)
这必须打印出 xyz.js
有没有办法呢?
答案 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"));