2012-01-18 82 views
1

作为跟进问题how-to-determine-source-information-of-callback-in-v8,我有以下问题。将sourceLocation属性添加到函数中?

如果我看一个函数的属性,我会看到它有一个名称,一个长度等等。是否可以通过黑客的'函数'对象的构造函数自动添加属性到所有函数?如果是这样,应该怎么做?我想添加一个名为'source_location'的属性

function foo() { 
} 

console.log(foo.name); //works out of the box 
console.log(foo.source_location); //can I make this work? 
+1

但'console.log'显示源位置了吗? – c69 2012-01-18 13:03:35

+0

是吗?你能给个例子吗? – coen 2012-01-18 13:58:33

+0

[Chrome](http://gyazo.com/69282b23aa0e4fe96958a7120e9bd644.png),[Opera](http://gyazo.com/d47e3e0848b613349ead9ece61278428.png),与Firefox和IE9相同。 – c69 2012-01-18 14:28:03

回答

0

您可以使用v8的调试器对象。

// test.js 

function bar() { 
} 

function foo() { 
} 

foo.source_location = debug.Debug.findFunctionSourceLocation(foo); 

console.log(foo.name); 
console.log(foo.source_location); 
console.log(foo.source_location.script.name); 

执行

node --expose-debug-as=debug test.js 

输出

foo 
{ script: {}, // script object 
    position: 106, 
    line: 5, 
    column: 12, 
    start: 94, 
    end: 110 } 
/home/skomski/test.js 

参考

http://code.google.com/p/v8/source/browse/trunk/src/debug-debugger.js#586