SORU
21 ŞUBAT 2009, CUMARTESİ


Nasıl JavaScript yok .prototip çalışması?

Dinamik programlama dilleri bir ilgim yok, ama JavaScript kodu payıma düşeni yazdım. Ben hiç kafamı prototip tabanlı programlama var, herhangi biri bu işlerin nasıl yürüdüğünü biliyor mu?

var obj = new Object(); // not a functional object
obj.prototype.test = function() { alert('Hello?'); }; // this is wrong!

function MyObject() {} // a first class functional object
MyObject.prototype.test = function() { alert('OK'); } // OK

Bir sürü tartışma insanlar, bir süre geri kaldım (tam olarak ne yaptığımdan emin değilim ama anladığım kadarıyla, bir sınıf kavramı yok hatırlıyorum. Sadece bir nesne ve bu nesnelerin örnekleri, orijinal klon, değil mi?

Ama JavaScript .prototype bu özelliğin tam olarak amacı nedir? Nasıl başlatmasını nesneler arasındaki ilişki nedir?


Edit

slides bunlar gerçekten bu konuyu anlamak için çok yardımcı oldu.

CEVAP
24 Ocak 2011, PAZARTESİ


Bir dil uygulama klasik miras gibi Java, C# veya C başlatın oluşturarak bir sınıf bir plan için nesneler ve o zaman sen-ebilmek yaratmak yeni nesneleri bu sınıf veya genişletmek sınıfı, tanımlama yeni bir sınıf zenginleştirerek özgün sınıf.

JavaScript bir nesne oluşturmak (sınıf kavramı yoktur), sonra da kendi nesne çoğaltmak ya da yeni nesneler oluşturabilirsiniz. Zor, ama biraz yabancı ve biri klasik yöntem için kullanılan metabolize etmek zor değil.

Örnek:

//Define a functional object to hold persons in JavaScript
var Person = function(name) {
  this.name = name;
};

//Add dynamically to the already defined object a new getter
Person.prototype.getName = function() {
  return this.name;
};

//Create a new object of type Person
var john = new Person("John");

//Try the getter
alert(john.getName());

//If now I modify person, also John gets the updates
Person.prototype.sayMyName = function() {
  alert('Hello, my name is '   this.getName());
};

//Call the new method on john
john.sayMyName();

Şimdi temel nesne uzanan durdum kadar, şimdi başka bir nesne ve o Kişi devralmasını istiyorum.

//Create a new object of type Customer by defining its constructor. It's not 
//related to Person for now.
var Customer = function(name) {
    this.name = name;
};

//Now I link the objects and to do so, we link the prototype of Customer to 
//a new instance of Person. The prototype is the base that will be used to 
//construct all new instances and also, will modify dynamically all already 
//constructed objects because in JavaScript objects retain a pointer to the 
//prototype
Customer.prototype = new Person();     

//Now I can call the methods of Person on the Customer, let's try, first 
//I need to create a Customer.
var myCustomer = new Customer('Dream Inc.');
myCustomer.sayMyName();

//If I add new methods to Person, they will be added to Customer, but if I
//add new methods to Customer they won't be added to Person. Example:
Customer.prototype.setAmountDue = function(amountDue) {
    this.amountDue = amountDue;
};
Customer.prototype.getAmountDue = function() {
    return this.amountDue;
};

//Let's try:       
myCustomer.setAmountDue(2000);
alert(myCustomer.getAmountDue());

var Person = function (name) {
    this.name = name;
};
Person.prototype.getName = function () {
    return this.name;
};
var john = new Person("John");
alert(john.getName());
Person.prototype.sayMyName = function () {
    alert('Hello, my name is '   this.getName());
};
john.sayMyName();
var Customer = function (name) {
    this.name = name;
};
Customer.prototype = new Person();

var myCustomer = new Customer('Dream Inc.');
myCustomer.sayMyName();
Customer.prototype.setAmountDue = function (amountDue) {
    this.amountDue = amountDue;
};
Customer.prototype.getAmountDue = function () {
    return this.amountDue;
};
myCustomer.setAmountDue(2000);
alert(myCustomer.getAmountDue());

SetAmountDue arayamam söylerken, getAmountDue() bir Kişi.

//The following statement generates an error.
john.setAmountDue(1000);

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ampisound

    Ampisound

    12 Kasım 2006
  • Incredible Tutorials

    Incredible T

    27 EKİM 2006
  • SelmerSaxMan

    SelmerSaxMan

    24 HAZİRAN 2006