2012-03-19 14 views
20

嚴格和非嚴格的代碼可以混合使用。但是即使呼叫者沒有嚴格的代碼,你也不能使用呼叫者。有人知道任何解決方法嗎?如何從嚴格模式獲取來電者?

我嘗試這樣做:

(function strict(){ 
    "use strict"; 
    nonStrict();//ok 
    nonStrictCaller();//error :(
})(); 

function nonStrict(){ 
    return 011;//Octal literals are not allowed in strict mode 
} 

function nonStrictCaller(){ 
    return nonStrictCaller.caller; 
} 
+1

你可以通過'this'的功能,不是嗎? – gdoron 2012-03-19 21:06:11

+0

我想你可能想看看這裏,特別是在評論http://stackoverflow.com/a/280396/575527 – Joseph 2012-03-19 21:20:42

+0

解決方法是讓你的代碼符合嚴格的模式。這就是嚴格模式的要點; *嚴格*。在你的情況下,這意味着通過其他方式訪問調用者,比如通過名稱作爲參數傳遞調用者。 – 2012-03-19 21:31:24

回答

11

這裏是一個邪惡的黑客工具,只能在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); 
    } 

    }(); 
} 
+3

這似乎不再適用於Chrome。 : - \ http://jsbin.com/zifadipivo/12/?v8Hack=true – 2015-03-18 18:57:32