Javascript mirası: süper kurucusunu çağırmak ya da prototip zinciri kullanmak?
Son zamanlarda MDC JavaScript çağrı kullanımı hakkında okudum
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call
örneğin bir linke, hala anlamıyorum, aşağıda gösterildiği.
Neden burada miras bu gibi kullanıyorlar
Prod_dept.prototype = new Product();
bu gerekli mi? Süper yapıcı bir çağrı var çünkü
Prod_dept()
her neyse, bu gibi
Product.call
bu sadece genel bir davranış değil mi? Ne zaman süper oluşturucu için arama kullanın ya da prototip zinciri kullanmak daha mı iyi?
function Product(name, value){
this.name = name;
if(value >= 1000)
this.value = 999;
else
this.value = value;
}
function Prod_dept(name, value, dept){
this.dept = dept;
Product.call(this, name, value);
}
Prod_dept.prototype = new Product();
// since 5 is less than 1000, value is set
cheese = new Prod_dept("feta", 5, "food");
// since 5000 is above 1000, value will be 999
car = new Prod_dept("honda", 5000, "auto");
Şeyleri daha net yapmak için teşekkürler
CEVAP
Asıl sorunun cevabı her ikisini de yapmak gerekir
- Üst başlatır bir örnek, prototip ayarı prototip zinciri (miras), bu sadece bir kez prototip nesnesi paylaşılır beri) yapılır.
- Ebeveynin yapıcı başlatır nesnenin kendisini arayıp, bu her örnekleme (her zaman bu yapı farklı parametreleri geçirebilirsiniz) ile yapılır.
Bu nedenle, miras kurarken ebeveynin kurucusunu çağırmak gerekir. Başka türeten bir nesne başlatmasını sadece.
Chris Morgan'ın cevabı neredeyse tam, küçük bir ayrıntı (yapıcı özelliği) eksik. Bana Kur miras için bir yöntem tavsiye edelim.
function extend(base, sub) {
// Avoid instantiating the base class just to setup inheritance
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
// for a polyfill
// Also, do a recursive merge of two prototypes, so we don't overwrite
// the existing prototype, but still maintain the inheritance chain
// Thanks to @ccnokes
var origProto = sub.prototype;
sub.prototype = Object.create(base.prototype);
for (var key in origProto) {
sub.prototype[key] = origProto[key];
}
// Remember the constructor property was set wrong, let's fix it
sub.prototype.constructor = sub;
// In ECMAScript5 (all modern browsers), you can make the constructor property
// non-enumerable if you define it like this instead
Object.defineProperty(sub.prototype, 'constructor', {
enumerable: false,
value: sub
});
}
// Let's try this
function Animal(name) {
this.name = name;
}
Animal.prototype = {
sayMyName: function() {
console.log(this.getWordsToSay() " " this.name);
},
getWordsToSay: function() {
// Abstract
}
}
function Dog(name) {
// Call the parent's constructor
Animal.call(this, name);
}
Dog.prototype = {
getWordsToSay: function(){
return "Ruff Ruff";
}
}
// Setup the prototype chain the right way
extend(Animal, Dog);
// Here is where the Dog (and Animal) constructors are called
var dog = new Dog("Lassie");
dog.sayMyName(); // Outputs Ruff Ruff Lassie
console.log(dog instanceof Animal); // true
console.log(dog.constructor); // Dog
Sınıfları oluştururken bile daha fazla şeker sözdizimsel için blog yazıma bakın. http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html
Teknik Ext-JS ve http://www.uselesspickles.com/class_library/ http://stackoverflow.com/users/1397311/ccnokes bir yorum kopyalanmış
'prototip' vs '' J...
Nasıl Javascript Çağırmak İçin Bir Bağ...
Nasıl JavaScript yok .prototip çalışma...
JavaScript: Sınıf.yöntem vs Sınıf.prot...
Nasıl Javascript bir değişkeni kullanm...