Ecco un trucco male che funziona solo in V8. La versione 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}
E la versione meno criptico
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);
}
}();
}
fonte
2012-09-10 08:38:31
È possibile passare 'this' alla funzione, non è vero? – gdoron
Penso che potresti voler guardare qui, specialmente nei commenti http://stackoverflow.com/a/280396/575527 – Joseph
La soluzione alternativa è rendere il tuo codice compatibile con la modalità rigorosa. Questo è il punto della modalità rigorosa; * è rigoroso *. Nel tuo caso, significherebbe rendere il chiamante accessibile con altri mezzi, come passarlo per nome come argomento. –