我需要一些關於javascript調用元數據的說明。下面提供的代碼具有SenderAsParameterFunc和NoExplicitParametersFunc,它們是從AnotherFunction的每個循環中調用的。javascript closure:隱式參數,調用者
var Obj = function(){
Obj.SenderAsParameterFunc = function(sender, param1, param2){
// log $(sender).id;
}
Obj.NoExplicitParametersFunc = function(){
// EXTRACT DEFAULT SENDER/CALLEE
// I see in console Obj.NoExplicitParametersFunc.caller.arguments >> SEE REF1
}
Obj.NoExplicitParametersFuncWithExtraArgs = function(this, par1, par2){
// 'this' from each loop?
}
Obj.AnotherFunc = function(){
var X = $('myselect');
var par1 = 1;
var par2 = 'bla';
$.each(X.find("option"), function() {
Obj.SenderAsParameterFunc(this, par1, par2);
// 'this' here references current object from 'each' loop
});
$.each(X.find("option"), Obj.NoExplicitParametersFunc);
// must be equvalent to first var,
// but to extract 'inner' this we have to do more magic in NoExplicitParametersFunc...
var par1 = 1;
var par2 = 'bla';
$.each(X.find("option"), Obj.NoExplicitParametersFuncWithExtraArgs);
}
}
那麼應該怎麼寫NoExplicitParametersFunc作爲在SenderAsParameterFunc與$(本)完成以訪問每個選項?
==========
OK,總結我學到的東西: 如果一個函數不用參數
$.each(X.find("option"), Obj.NoExplicitParametersFunc);
然後 '這' 默認情況下,通過並且可以在一個函數內訪問。
如果函數參數
$.each(X.find("option"), function() { Obj.SenderAsParameterFunc(this, par1, par2); });
然後「這個」必須包含作爲參數(第一?)是函數內訪問。
=============
REF1:
b.Event
altKey: undefined
attrChange: undefined
attrName: undefined
bubbles: true
cancelable: false
ctrlKey: undefined
currentTarget: input#inputId
data: undefined
delegateTarget: input#inputId
eventPhase: 2
handleObj: Object
isDefaultPrevented: function ot(){return!1}
jQuery19105656534675508738: true
metaKey: false
originalEvent: Event
relatedNode: undefined
relatedTarget: undefined
shiftKey: undefined
srcElement: input#inputId
target: input#inputId
timeStamp: 1366622725473
type: "change"
view: undefined
which: undefined
__proto__: Object,
currentTarget當前,delegateTarget,srcElement,目標相同,但應該怎樣實際使用,最正確的辦法?參考將不勝感激!
謝謝。
'REF1'看起來不像您提供的代碼的結果。它是一個jQuery事件對象。 – 2013-04-22 10:58:11
WTH是'Obj.caller.arguments'應該是? – Bergi 2013-04-22 11:01:34
這是Obj.NoExplicitParametersFunc.caller.arguments,如果它不假設做任何事情,那麼讓我們關注一下什麼可以和將會發生什麼。在console.log(this,i,v)中爲 – 2013-04-22 11:14:39