자바스크립트 날짜 프로토타입
예시
myName이라는 월 이름 속성을 날짜 개체에 제공하는 새 날짜 메서드를 만듭니다.
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"};
}
Date 객체를 만든 다음 myMonth 메서드를 호출합니다.
const d = new Date();
let month = d.myMonth();
정의 및 사용
prototype
날짜에 새 속성과 메서드를 추가할 수 있습니다.
prototype
모든 JavaScript 개체에서 사용할 수 있는 속성입니다.
브라우저 지원
prototype
ECMAScript1(ES1) 기능입니다.
ES1(JavaScript 1997)은 모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
통사론
Date.prototype.name = value
경고
제어하지 않는 개체의 프로토타입을 변경하지 않는 것이 좋습니다.
다음과 같은 내장 JavaScript 데이터 유형의 프로토타입을 변경해서는 안 됩니다.
- 번호
- 문자열
- 배열
- 날짜
- 부울
- 기능
- 사물
자신의 개체의 프로토타입만 변경하십시오.
프로토타입 속성
JavaScript prototype
속성을 사용하면 개체에 새 속성을 추가할 수 있습니다.
예시
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";