【发布时间】:2016-03-08 15:11:16
【问题描述】:
var fun1=function(){console.log('hello');}
var fun2=fun1
console.log(fun2);
上面的代码在 Firefox 中运行会打印出fun2。在 Chrome 中它打印函数体,在 Node.js 中它打印 Function。
为什么会有这种差异?
如何在 Node.js 中获取 Firefox 的行为?
我为什么要问这个?
我问这个是因为我想调试从 Idris 生成的 JS 代码,其中 JS 运行时使用 explicit 调用堆栈,并且我想以有意义的方式打印调用堆栈内容,而 Firefox 做得最好,但我想在 Node.js 上调试代码,因此我想让 Node.js 将函数打印为 Firefox,我该怎么做?
编辑:
一个典型的编译函数如下所示:
var _idris__123_io_95_bind2_125_ = function(oldbase){
var myoldbase = new i$POINTER();
i$valstack_top += 1;
i$ret = new i$CON(65646,[i$valstack[i$valstack_base],i$valstack[i$valstack_base + 1],i$valstack[i$valstack_base + 2],i$valstack[i$valstack_base + 3],i$valstack[i$valstack_base + 4],i$valstack[i$valstack_base + 5]],_idris__123_APPLY0_125_$65646,null);
i$valstack_top = i$valstack_base;
i$valstack_base = oldbase.addr;
}
所以这里有用的信息是变量名_idris__123_io_95_bind2_125_本身,这是Firefox打印的,而不是node.js打印的,这就是问题所在,Firfox打印有用的信息,node.js确实不是。
所以问题是,我怎样才能让node.js 为上述功能打印_idris__123_io_95_bind2_125_?
编辑 2:
不幸的是,尝试一些建议不起作用:
>cat deb.js
var fun1=function(){console.log('hello');}
var fun2=fun1
console.log(fun2);
console.log(fun2.name);
console.log(fun2.toString());
console.log(fun2+'');
>node deb.js
[Function]
function (){console.log('hello');}
function (){console.log('hello');}
>
【问题讨论】:
-
试试
console.log(fun2 + '');
标签: javascript node.js firefox-developer-tools idris