1

プラグインがどのように機能するかを理解できるように、プラグインを調べて文書化しようとしていますが、理解できない方法で .trigger を使用する関数に出くわしました。私が知っていると思うことから、.trigger は通常、カスタム イベントを呼び出すために使用されます。プラグインでのトリガーの使用例は次のとおりです。

$( this ).trigger( "beforecreate." + pluginName )
[ pluginName ]( "_init" )
[ pluginName ]( "_addNextPrev" )
.trigger( "create." + pluginName );

これが何を言っているのか誰か説明できますか?構文は jQuery のドキュメントにあるものとは異なるため、何らかの省略表現か何かであると想定しています。

4

2 に答える 2

2

[...]についてではなく について混乱していると思います.trigger。JavaScript では、オブジェクト プロパティにアクセスする方法が 2 つあります。ドット表記 ( obj.someProp) とブラケット表記 ( obj['someProp']) です。
プロパティ名が有効な識別子ではない場合 (たとえば、スペースが含まれている場合)、または変数にプロパティ名がある場合は、ブラケット表記を使用する必要があります。

と仮定しましょう

var pluginName = 'foo';

上記のコードは次と同等です。

$( this )
  .trigger( "beforecreate." + pluginName )
  .foo( "_init" )
  .foo( "_addNextPrev" )
  .trigger( "create." + pluginName );

これは別の(構成された)例です。これらはどちらも同等です。

$(this).find('.foo');
$(this)['find']('.foo');
于 2012-08-27T14:38:19.097 に答える
1

簡単に言えば:

//  This is triggering the "beforecreate" EVENT on the plugin
$( this ).trigger( "beforecreate." + pluginName )
//  is calling the initialize event within the class
[ pluginName ]( "_init" )
//  i assume is calling another method in the class, probably to set specific html to the element being modded
[ pluginName ]( "_addNextPrev" )
//  Now that everything has been setup and the element has been "Created" into the plugin,
//    the coder is now triggering the plugin upon that element
.trigger( "create." + pluginName );
于 2012-08-27T14:32:52.007 に答える