SORU
20 EYLÜL 2011, Salı


JavaScript Miras

Javascript miras kullanmaya çalışıyorum. Minimum kod desteklemek için aşağıdaki geldim.

function Base(){
    this.call = function(handler, args){
      handler.call(this, args);
    }
}

Base.extend = function(child, parent){
    parent.apply(child);
    child.base = new parent;
    child.base.child = child;
}

Eğer bu kaçırmış olabilirim yeterli ya da herhangi bir diğer önemli sorun olacak olursa, lütfen bana bildirin uzmanlar. Benzer sorunlarla karşı karşıya dayanarak lütfen diğer değişiklikler göstermektedir.

İşte tam test komut:

function Base(){
    this.call = function(handler, args){
      handler.call(this, args);
    }
    this.superalert = function(){
        alert('tst');
    }
}

Base.extend = function(child, parent){
    parent.apply(child);
    child.base = new parent;
    child.base.child = child;
}

function Child(){
    Base.extend(this, Base);
    this.width = 20;
    this.height = 15;
    this.a = ['s',''];
    this.alert = function(){
        alert(this.a.length);
        alert(this.height);
    }
}

function Child1(){
    Base.extend(this, Child);
    this.depth = 'depth';
    this.height = 'h';
    this.alert = function(){
        alert(this.height); // display current object height
        alert(this.a.length); // display parents array length
        this.call(this.base.alert); 
          // explicit call to parent alert with current objects value
        this.call(this.base.superalert); 
          // explicit call to grandparent, parent does not have method 
        this.base.alert(); // call parent without overriding values
    }
}

var v = new Child1();
v.alert();
alert(v.height);
alert(v.depth);

CEVAP
10 EKİM 2012, ÇARŞAMBA


ECMAScript 5 javascript miras uygulamak için bir nesnenin prototip tanımlayabilirsiniz ve kullanım Object.create miras. Ayrıca/geçersiz kılma özellikleri istediğiniz kadar ekleyebilirsiniz.

Örnek:

/**
 * Transform base class
 */
function Transform() {
    this.type = "2d";
}

Transform.prototype.toString = function() {
    return "Transform";
}

/**
 * Translation class.
 */
function Translation(x, y) {
    // Parent constructor
    Transform.call(this);

    // Public properties
    this.x = x;
    this.y = y;
}

// Inheritance
Translation.prototype = Object.create(Transform.prototype);

// Override
Translation.prototype.toString = function() {
    return Transform.prototype.toString()   this.type   " Translation "   this.x   ":"   this.y;
}

/**
 * Rotation class.
 */
function Rotation(angle) {
    // Parent constructor
    Transform.call(this);

    // Public properties
    this.angle = angle;
}

// Inheritance
Rotation.prototype = Object.create(Transform.prototype);

// Override
Rotation.prototype.toString = function() {
    return Transform.prototype.toString()   this.type   " Rotation "   this.angle;
}

// Tests
translation = new Translation(10, 15);

console.log(translation instanceof Transform); // true
console.log(translation instanceof Translation); // true
console.log(translation instanceof Rotation); // false

console.log(translation.toString()) // Transform2d Translation 10:15

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Marques Brownlee

    Marques Brow

    21 Mart 2008
  • paikimchung

    paikimchung

    12 Mayıs 2006
  • TheGamer2323

    TheGamer2323

    25 Ocak 2009