假设我将 jQuery 扩展方法定义为:
$.fn.myMethod = function(text)
{
$(this).text(text); ///
}
我也可能有类似的方法:
$.fn.myMethod2 = function(text)
{
$(this).text(text + " superduper");
}
现在假设我有一些看起来像这样的配置类:
var myConfig =
{
myMethod: /// this should be delegate to one of the above
}
我的目标是在我的代码中的某个地方,我可能会输入如下内容:
$("selector").myConfig.myMethod("Insert this");
基本上 myConfig.myMethod 是委托给 myMethod 或 myMethod2。我经常用我自己的函数来做这件事,但我不确定如何用 jQuery 方法正确地实现他,我更感兴趣的是学习正确的方法,而不是试验和可能造成错误的东西。