SORU
18 ŞUBAT 2009, ÇARŞAMBA


Temel arama yöntemi kullanarak prototip JavaScript

Eğer geçersiz ise JavaScript prototip bir yöntemini temel yöntem aramak mümkün mü?

MyClass = function(name){
    this.name = name;
    this.do = function() {
        //do somthing 
    }
};

MyClass.prototype.do = function() {  
    if (this.name === 'something') {
        //do something new
    } else {
        //CALL BASE METHOD
    }
};

CEVAP
18 ŞUBAT 2009, ÇARŞAMBA


Yapmaya çalıştığın şeyi anlamadım, ama normalde nesneye özgü davranışları uygulama bu doğrultuda yapılır:

function MyClass(name) {
    this.name = name;
}

MyClass.prototype.doStuff = function() {
    // generic behaviour
}

var myObj = new MyClass('foo');

var myObjSpecial = new MyClass('bar');
myObjSpecial.doStuff = function() {
    // do specialised stuff
    // how to call the generic implementation:
    MyClass.prototype.doStuff.call(this /*, args...*/);
}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Dion Coulls

    Dion Coulls

    16 AĞUSTOS 2006
  • george sarintzotis

    george sarin

    2 Aralık 2007
  • Rooster Teeth

    Rooster Teet

    11 Temmuz 2006