0

次のコード行を読んでいました

  items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source

bootstrap-typeahead.js v2.1.0から。

誰かが私に
どのように機能するか説明できますかthis.source(this.query, $.proxy(this.process, this))?

確かに:
1)this.source(1)で定義された関数を参照していると思いますか?


(1)

    element.typeahead({
        minLength: 3,
        source: function () {
             // some code
        }
    });
4

1 に答える 1

1

それを壊すには:items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source;

したがって、オブジェクトには source と呼ばれるプロパティがあり、配列または関数のthis.sourceように見えます。this.sourceそれらの述語関数$.isFunction(this.source)はオブジェクトを取り、それが関数の場合は true を返します。関数の場合はthis.source(this.query, $.proxy(this.process, this))実行されます。

ここで、this.source(this.query, $.proxy(this.process, this))通話を中断します。

$.isFunction(this.source)関数である呼び出しの結果から、 this.source2 つの引数を取ることがわかります。最初this.queryは文字列だと思います。2 つ目$.proxy(this.process, this))は、コールバック関数です。関数とオブジェクト/コンテキストの 2 つの$.proxy引数を取り、新しい関数を返します。返された関数コンテキスト ( this) は、確実にオブジェクト/コンテキストで渡されます。

$.proxyこのように見えます

var proxy = function( func, context ) {
    return ( function() {
        func.apply( context, arguments );
    });
};

//build a new function and set this to window
var newFunction = proxy( someOrtherFunction, window );

//use the new function
newFunction();

ここで詳細を準備でき$.proxyます:http://api.jquery.com/jQuery.proxy/

から生成された新しく作成された関数は、コールバックとして関数$.proxy(this.process, this)で使用されます。this.source

于 2012-08-28T17:15:36.350 に答える