我想使用uglify js 2解析一些JavasScript代码来列出给定“类”的所有方法。在我的示例中,TreeWalker返回一个带有name : null的节点,并且没有允许父节点得出结论的信息。
有谁知道不同的方法吗?我期待着像name : "Test.method_name"这样的东西
到目前为止我试着翻翻..。
parsetests.js
var UglifyJS = require("uglify-js2");
var util = require("util");
var code = require("fs").readFileSync("test.js").toString();
var toplevel = UglifyJS.parse(code);
var log = function(obj, depth) {
console.log(util.inspect(obj, showHidden=false, depth, colorize=true));
};
var toplevel = UglifyJS.parse(code);
var walker = new UglifyJS.TreeWalker(function(node){
if (node instanceof UglifyJS.AST_Function ) {
log(node, 2);
}
});
toplevel.walk(walker);test.js
function Test(argument1) {
var m = argument1 + "test";
return this;
}
Test.prototype.method_name = function(first_argument) {
// body...
return "a";
};UglifyJS.TreeWalker节点:
{ end:
{ file: null,
comments_before: [],
nlb: true,
endpos: 156,
pos: 155,
col: 0,
line: 10,
value: '}',
type: 'punc' },
start:
{ file: null,
comments_before: [],
nlb: false,
endpos: 111,
pos: 103,
col: 29,
line: 7,
value: 'function',
type: 'keyword' },
body:
[ { end: [Object],
start: [Object],
value: [Object] } ],
cname: undefined,
enclosed: undefined,
parent_scope: undefined,
uses_eval: undefined,
uses_with: undefined,
functions: undefined,
variables: undefined,
directives: undefined,
uses_arguments: undefined,
argnames:
[ { end: [Object],
start: [Object],
thedef: undefined,
name: 'first_argument',
scope: undefined,
init: undefined } ],
name: null }发布于 2015-04-14 14:39:43
在您的示例中,函数没有名称,而是分配给具有名称的属性。必须将您的功能命名如下:
Test.prototype.method_name = function method_name(first_argument) {
// body...
return "a";
};https://stackoverflow.com/questions/29488449
复制相似问题