我有一個封閉件,和從該封閉件,我需要訪問arguments
對象文字的功能,其中所述封閉件限定:如何從閉包訪問外部函數的「參數」字面值對象?
function outerFunction(){
var myClosure = function(){
// I need to access the "arguments" object of "outerFunction"
}
}
這是可能的,而不在變量存儲arguments
?
我有一個封閉件,和從該封閉件,我需要訪問arguments
對象文字的功能,其中所述封閉件限定:如何從閉包訪問外部函數的「參數」字面值對象?
function outerFunction(){
var myClosure = function(){
// I need to access the "arguments" object of "outerFunction"
}
}
這是可能的,而不在變量存儲arguments
?
簡短回答:只需使用一個變量。沒有很好的理由不是到。
這可能沒有存儲變量的參數?
你的選擇是要麼將其存儲到arguments
對象本身的引用,或使用變量(或參數)來指代從它的個別項目,但你不能訪問arguments
對象本身,你的外在的因爲它自己的arguments
會影響它。
有一個非常有限的情況是,你可以做到這一點沒有做任何的那些東西:在調用outerFunction
(不晚,如果myClosure
生存outerFuntion
返回),你可以使用outerFunction.arguments
。我不認爲這是記錄行爲(至少,我不能在規範中找到它),但它適用於Chrome,Firefox和IE11。例如:
function outerFunction() {
var myClosure = function(where) {
snippet.log(where + " " + JSON.stringify(outerFunction.arguments));
};
myClosure("Inside");
return myClosure;
}
var c = outerFunction(1, 2, 3);
c("Outside");
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
我看到沒有理由這樣做,並再次我不認爲它實際上是在規範(我認爲這是不確定的行爲的一些代碼的方式回到依靠和所以瀏覽器複製)。但它至少在一些引擎上工作,只要你這樣做期間呼叫outerFunction
。
在評論你直接以automatise任務
這也正是它賦值給一個變量確實爲你說
我想稱呼它,它使得有可能從內函數內的外部函數的
arguments
使用:function outerFunction() { var args = arguments; var myClosure = function() { snippet.log(JSON.stringify(args)); }; return myClosure; } var c = outerFunction(1, 2, 3); c();
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>