1

我需要对 javascript 调用的元数据进行一些说明。下面提供的代码具有 SenderAsParameterFunc 和 NoExplicitParametersFunc,它们是从 AnotherFunction 的每个循环中调用的。

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 中使用 $(this) 完成的那样?

==========

好的,总结一下我学到的东西:如果一个函数没有参数

$.each(X.find("option"), Obj.NoExplicitParametersFunc);

然后'this'默认传递并且可以在函数中访问。

如果函数有参数

$.each(X.find("option"), function () { Obj.SenderAsParameterFunc(this, par1, par2); });

那么 'this' 必须作为参数包含在函数中(首先?)才能在函数中访问。

==============

参考1:

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、target 是一样的,但实际应该使用什么,最正确的方法是什么?参考将不胜感激!

谢谢。

4

2 回答 2

1

TL;DR:在您的情况下,这并不重要,但如果您有疑问,请使用currentTarget


我真的不明白你的第一个问题,但关于第二个......

当一个事件被调度时,这个事件通常会冒泡,例如在这个页面上:

<body>
  <div>
    <img src="foobar.png" />
  </div>
</body>

如果用户单击 aimg内的元素div,它将以这种方式触发事件

body - capturing
div - capturing
img - at target
div - bubbling
body - bubbling

因此,您可以添加一个侦听器body并捕获该事件。但是为了让您知道实际单击了哪个元素(img在这种情况下),它提供了event.target属性(srcElement在 Internet Explorer 上)。

delegateTarget是 jQuery 添加的属性,如果您使用事件委托,它会提供额外的信息。我不知道它到底是什么,但你可以看看文档

最后currentTarget是您附加事件的元素,如果我们将侦听器添加到body它将是body. 所以每个事件的论点是

fire body.click with { target: img, currentTarget: body }
fire div.click  with { target: img, currentTarget: div  }
fire img.click  with { target: img, currentTarget: img  }
fire div.click  with { target: img, currentTarget: div  }
fire body.click with { target: img, currentTarget: body }

根据您在做什么,您将需要一个或另一个属性。

于 2013-04-22T11:50:26.027 回答
1
$.each(X.find("option"), function () {
      Obj.SenderAsParameterFunc(this);
      // 'this' here references anonymous function, 
      // from which it was called, so 'this' is sender
});

不。匿名函数在这里没有被任何东西引用(它只能在 中作为参数访问$.each)。回调中的this关键字$.each确实引用了迭代项。这也确实回答了您的问题:

Obj.NoExplicitParametersFunc = function(i, v){
    console.log(this, i, v);
}

如果一个函数有参数[并且在匿名函数表达式中被调用],那么必须将“this”作为参数包含在函数中(首先?)才能在函数中访问。

是的,由于this每个函数调用都是独一无二的,您必须传递它的值 - 就像您不能只访问调用者函数的局部变量一样,您也不能访问它的this值。要使用特定的 this 值调用函数,可以使用.call

$.each(X.find("option"), function(i, v) {
    Obj.SenderAsParameterFunc.call(this, i, v, par1, par2);
});
于 2013-04-22T10:59:43.747 回答