JavaScript iyi bir Örnek'In Prototip Tabanlı bir Miras
10 yılı aşkın süredir OOP dilleri ile programlama olmamıştı ama şimdi JavaScript öğreniyorum ve prototip tabanlı bir miras karşılaştığım ilk defa. İyi kodu inceleyerek en hızlı öğrenmek eğilimindedir. Düzgün ilk miras kullanan JavaScript bir uygulama (veya kitaplığı) iyi yazılmış bir örnek nedir? Ve (kısaca) okumaya başla girebilmem için ilk miras kullanılır nerede/nasıl, tarif EDEBİLİR MİSİN?
CEVAP
Belirtildiği gibi, Douglas Crockford tarafından sinemaya neden ve kapakları nasıl iyi bir açıklama. Ama JavaScript, birkaç satır koymak için:
// Declaring our Animal object
var Animal = function () {
this.name = 'unknown';
this.getName = function () {
return this.name;
}
return this;
};
// Declaring our Dog object
var Dog = function () {
// A private variable here
var private = 42;
// overriding the name
this.name = "Bello";
// Implementing ".bark()"
this.bark = function () {
return 'MEOW';
}
return this;
};
// Dog extends animal
Dog.prototype = new Animal();
// -- Done declaring --
// Creating an instance of Dog.
var dog = new Dog();
// Proving our case
console.log(
"Is dog an instance of Dog? ", dog instanceof Dog, "\n",
"Is dog an instance of Animal? ", dog instanceof Animal, "\n",
dog.bark() "\n", // Should be: "MEOW"
dog.getName() "\n", // Should be: "Bello"
dog.private "\n" // Should be: 'undefined'
);
Bu yaklaşım ile sorun, ancak, bir oluşturmak için yeniden oluşturmak nesne her zaman olacaktır. Başka bir yaklaşım prototip üzerinde nesneler yığını gibi ilan etmek
// Defining test one, prototypal
var testOne = function () {};
testOne.prototype = (function () {
var me = {}, privateVariable = 42;
me.someMethod = function () {
return privateVariable;
};
me.publicVariable = "foo bar";
me.anotherMethod = function () {
return this.publicVariable;
};
return me;
}());
// Defining test two, function
var testTwo = ​function() {
var me = {}, privateVariable = 42;
me.someMethod = function () {
return privateVariable;
};
me.publicVariable = "foo bar";
me.anotherMethod = function () {
return this.publicVariable;
};
return me;
};
// Proving that both techniques are functionally identical
var resultTestOne = new testOne(),
resultTestTwo = new testTwo();
console.log(
resultTestOne.someMethod(), // Should print 42
resultTestOne.publicVariable // Should print "foo bar"
);
console.log(
resultTestTwo.someMethod(), // Should print 42
resultTestTwo.publicVariable // Should print "foo bar"
);
// Performance benchmark start
var stop, start, loopCount = 1000000;
// Running testOne
start = (new Date()).getTime();
for (var i = loopCount; i>0; i--) {
new testOne();
}
stop = (new Date()).getTime();
console.log('Test one took: ' Math.round(((stop/1000) - (start/1000))*1000) ' milliseconds');
// Running testTwo
start = (new Date()).getTime();
for (var i = loopCount; i>0; i--) {
new testTwo();
}
stop = (new Date()).getTime();
console.log('Test two took: ' Math.round(((stop/1000) - (start/1000))*1000) ' milliseconds');
İçgözlem gelince hafif bir dezavantajı var. Damping testOne, daha az yararlı bilgiler neden olur. Ayrıca özel mülkiyet "" testOne "tüm örneklerini, yardımsever shesek tarafından yanıt bahsedilen als. paylaşılan" privateVariable

prototip sınıfı vs göre miras tabanlı...
Bu bir Javascript prototip dili tabanl...
Ne .JavaScript: geniÅŸletmek ve .protot...
JavaScript "Dizi(n)" yeni; ve...
Bir JavaScript başlatmasını prototip ç...