2013-05-15 35 views
7

"javascript olarak , her nesne bir zincir. Bir nesne olmayan bir özellik için sorulduğunda üst nesnesidir oluşturan, oluşturulduğu nesneye bir gizli bağlantıya sahip olan sordu ... mülkiyet bulundu kadar veya kök nesneye ulaşana kadar sürekli zincir kadar. "Yöntem kalıtım

Her zaman, yukarıdaki sözcüklerin şimdi bile gerçek olduğunu düşünüyorum, Bu yüzden doğrulamak için bazı testler yaptım, Aşağıdaki gibi nesnelerin ilişkisini tanımlamak için tasarlanmıştır. lütfen onu gözden geçir .

enter image description here

Aşağıdaki kod gibi görünmelidir.

 //Shape - superclass 
     function Shape() { 
      this.x = 0; 
      this.y = 0; 
     }; 

     Shape.prototype.move = function(x, y) { 
      this.x += x; 
      this.y += y; 

      alert('Shape move'); 
     }; 

     // Rectangle - subclass 
     function Rectangle() { 
      Shape.call(this); //call super constructor. 
     } 

     Rectangle.prototype.move = function(x, y) { 
      this.x += x; 
      this.y += y; 

      alert('Rectangle move'); 
     }; 

     // Square - subclass 
     function Square(){ 
      Shape.call(this); 
     } 

     Rectangle.prototype = Object.create(Shape.prototype); 
     Square.prototype=Object.create(Rectangle.prototype); 

     var rect = new Rectangle(); 

     var sq= new Square(); 

     sq.x=1; 
     sq.y=1; 
     sq.move(1,1); 

move yöntem Square.prototype içinde bulunamaz, So JavaScript zinciri, ben bu Rectangle.prototype bulunacaktır düşünmüştü, ama aslında o bulunursa aşağıdaki üst nesneleri bulacaksınız yana sq.move(1,1) aslında Rectangle.prototype ait move yöntemi çağırmak yerine Shape.prototype.move dememizin kök Shape.prototype, Ben anlayamıyorum nedir? Bir şey özledim mi teşekkürler.

+2

Gerekir yerine Şekli 'den' Kare() 'contructor çağrı' Dikdörtgen() '()' üzerine yazılır? 'Square' yerine' Shape' alt sınıf bırak ben deneyeyim @all bir 'Rectangle' alt sınıf (?) – nnnnnn

+0

Çünkü, ben sorun olmadığını düşünüyorum. – DaGLiMiOuX

+0

olduğu –

cevap

5

Sadece üzerine senin zaten move vardı Rectangle.prototype. Üzerine yazdığınıza göre, eklediğiniz move artık orada değil, bu yüzden Shape'un hareketi kullanılıyor.

Rectangle.prototype.move = function(x, y) { 
    this.x += x; 
    this.y += y; 
    alert('Rectangle move'); 
}; 

function Square(){ 
    Shape.call(this); 
} 

//overwritten the prototype 
Rectangle.prototype = Object.create(Shape.prototype); 

Önce eklemeden önce prototip nesnesini oluşturun.

Rectangle.prototype = Object.create(Shape.prototype); 
Rectangle.prototype.move = function (x, y) { 
    this.x += x; 
    this.y += y; 
    alert('Rectangle move'); 
}; 
+1

Bunun kaldırma ile hiçbir ilgisi yoktur; kod sadece 'Rectangle.prototype' üzerine yazıyor :) –

+0

@Jack Oh evet. Kodu çok karışıktı, başlangıçta düşündüm. Teşekkürler – Joseph

+0

+1 Her zaman müthişsiniz! –

2

Prototip uzantısını aşağı taşıyabilirsiniz. Şimdi bunu uzanan sonra prototip atama vardır, bu nedenle genişletilmiş bir

//Shape - superclass 
     function Shape() { 
      this.x = 0; 
      this.y = 0; 
     }; 
     // Rectangle - subclass 
     function Rectangle() { 
      Shape.call(this); //call super constructor. 
     } 
     // Square - subclass 
     function Square(){ 
      Shape.call(this); 
     }  

     Rectangle.prototype = Object.create(Shape.prototype); 
     Square.prototype = Object.create(Rectangle.prototype); 

     Shape.prototype.move = function(x, y) { 
      this.x += x; 
      this.y += y; 

      alert('Shape move'); 
     }; 
     Rectangle.prototype.move = function(x, y) { 
      this.x += x; 
      this.y += y; 

      alert('Rectangle move'); 
     }; 

     var rect = new Rectangle(); 
     var sq = new Square(); 

     sq.x=1; 
     sq.y=1; 
     sq.move(1,1);