這裏是一個邪惡的黑客工具,只能在V8。該140 bytes
版本:
function x(a,b,c){function d(e,f){d=f}c=(b=Error)[a='prepareStackTrace'];b.captureStackTrace(b[a]=d,x);d.stack;b[a]=c;return d}
和較隱蔽的版本
if ('captureStackTrace' in Error) {
void function(){
function prepare(e, callsites){
return callsites;
}
function stack(f){
var e = {};
var oldPrepare = Error.prepareStackTrace;
Error.prepareStackTrace = prepare;
Error.captureStackTrace(e, f || stack.caller);
e = e.stack;
Error.prepareStackTrace = oldPrepare;
return e;
}
function lastReceiver(){
return stack(lastReceiver)[2].receiver;
}
var CallSite = stack()[0].constructor;
var callsiteMethods = {};
Object.getOwnPropertyNames(CallSite.prototype).forEach(function(key){
if (/^is|^get/.test(key)) {
callsiteMethods[key.replace(/^is|^get/, '')] = CallSite.prototype[key];
}
callsiteMethods.location = CallSite.prototype.toString;
});
CallSite.prototype.resolve = function resolve(){
for (var k in callsiteMethods)
this[k] = callsiteMethods[k].call(this);
}
}();
}
你可以通過'this'的功能,不是嗎? – gdoron 2012-03-19 21:06:11
我想你可能想看看這裏,特別是在評論http://stackoverflow.com/a/280396/575527 – Joseph 2012-03-19 21:20:42
解決方法是讓你的代碼符合嚴格的模式。這就是嚴格模式的要點; *嚴格*。在你的情況下,這意味着通過其他方式訪問調用者,比如通過名稱作爲參數傳遞調用者。 – 2012-03-19 21:31:24