SORU
28 Temmuz 2010, ÇARŞAMBA


bir WordPress kullanmak.() proxy kullanımı

jQuery.proxy() apı okuyordum. Ama bu en iyi kullanım nedir merak ettim umut verici görünüyor. Biri beni aydınlatabilir mi?

CEVAP
28 Temmuz 2010, ÇARŞAMBA


Fonksiyonu istediğiniz zaman this değeri belirli bir nesneye bağlı. Olay işleyicileri, AJAX geri aramalar, zaman aşımı, aralıklarla, özel nesneler, vb gibi geri örneğin,.

Bu yararlı olabilir, bir durum ile üretilmiş bir örnektir. Person bir nesne yok varsayarak bir özellik adı vardır. Ayrıca bir metin giriş elemanı bağlanır ve giriş değeri değiştiğinde, bu kişi, nesne adı da güncellenmiş olur.

function Person(el) {
    this.name = '';

    $(el).change(function(event) {
        // Want to update this.name of the Person object,
        // but can't because this here refers to the element
        // that triggered the change event.
    });
}

Sık sık kullanan bir çözüm bir değişken bu bağlamda mağaza ve geri çağırma işlevi gibi içinde kullanımı:

function Person(el) {
    this.name = '';

    var self = this; // store reference to this

    $(el).change(function(event) {
        self.name = this.value; // captures self in a closure
    });
}

Alternatif olarak, 10* *referansı Kişinin nesne yerine olayı tetikleyen unsuru ifade eder yani jQuery.proxy burada kullanabilirdik.

function Person(el) {
    this.name = '';

    $(el).change(jQuery.proxy(function(event) {
        this.name = event.target.value;
    }, this));
}

Bu özellik şimdi bind yöntemi Prototip ödünç içerir ve zaten bazı tarayıcılarda mevcut olan ECMA 5 standart olduğunu unutmayın.

function Person(el) {
    this.name = '';

    $(el).change(function(event) {
        this.name = event.target.value;
    }.bind(this)); // we're binding the function to the object of person
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • SavageLuxxxGaming - Daily NBA 2K15 & NBA Live 15 Enjoy And Subscribe!

    SavageLuxxxG

    11 Ocak 2013
  • Sorikan

    Sorikan

    3 ŞUBAT 2008
  • Tome Rodrigo

    Tome Rodrigo

    9 Temmuz 2006