JavaScript Tarih prototipi
Örnek
Tarih nesnesine myName adında bir ay-adı özelliği veren yeni bir tarih yöntemi yapın:
Date.prototype.myMonth = function()
{
if (this.getMonth()==0) {return "January"};
if (this.getMonth()==1) {return "February"};
if (this.getMonth()==2) {return "March"};
if (this.getMonth()==3) {return "April"};
if (this.getMonth()==4) {return "May"};
if (this.getMonth()==5) {return "June"};
if (this.getMonth()==6) {return "July"};
if (this.getMonth()==7) {return "August"};
if (this.getMonth()==8) {return "September"};
if (this.getMonth()==9) {return "October"};
if (this.getMonth()==10) {return "November"};
if (this.getMonth()==11) {return "December"};
}
Bir Date nesnesi yapın, ardından myMonth yöntemini çağırın:
const d = new Date();
let month = d.myMonth();
Tanım ve Kullanım
prototype
tarihlere yeni özellikler ve yöntemler eklemenizi sağlar.
prototype
tüm JavaScript nesneleri ile kullanılabilen bir özelliktir.
Tarayıcı Desteği
prototype
bir ECMAScript1 (ES1) özelliğidir.
ES1 (JavaScript 1997) tüm tarayıcılarda tam olarak desteklenir:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
Sözdizimi
Date.prototype.name = value
Uyarı
Kontrol etmediğiniz bir nesnenin prototipini değiştirmeniz önerilmez.
Aşağıdakiler gibi yerleşik JavaScript veri türlerinin prototipini değiştirmemelisiniz:
- sayılar
- Teller
- diziler
- Tarih
- Boole değerleri
- İşlev
- nesneler
Yalnızca kendi nesnelerinizin prototipini değiştirin.
Prototip Özellik
JavaScript prototype
özelliği, nesnelere yeni özellikler eklemenize olanak tanır:
Örnek
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";