JavaScript Date 프로토타입 속성

정의와 사용법

prototype 생성자 함수는 Date() 객체에 새로운 속성과 메서드를 추가할 수 있게 합니다.

생성자 속성에서 모든 날짜 객체는 속성과 그 값이 기본 값으로 할당됩니다.

생성자에서 모든 날짜 객체는 이 메서드를 사용할 수 있습니다.

주의事项:Date.prototype는 단일 날짜 객체를 참조하는 것이 아니라 Date() 객체 자체를 참조합니다。

주의事项:Prototype는 모든 JavaScript 객체에 적용되는 전역 객체 생성자입니다。

인스턴스

새로운 날짜 메서드를 생성하여 날짜 객체에 "myProp"라는 month-name 속성을 제공하십시오:

Date.prototype.myMet = function() {
  if (this.getMonth() == 0){this.myProp = "January"};
  if (this.getMonth() == 1){this.myProp = "February"};
  if (this.getMonth() == 2){this.myProp = "March"};
  if (this.getMonth() == 3){this.myProp = "April"};
  if (this.getMonth() == 4){this.myProp = "May"};
  if (this.getMonth() == 5){this.myProp = "June"};
  if (this.getMonth() == 6){this.myProp = "July"};
  if (this.getMonth() == 7){this.myProp = "August"};
  if (this.getMonth() == 8){this.myProp = "September"};
  if (this.getMonth() == 9){this.myProp = "October"};
  if (this.getMonth() == 10){this.myProp = "November"};
  if (this.getMonth() == 11){this.myProp = "December"};
};

Date 객체를 생성한 후 myMet 메서드를 호출하십시오:

var d = new Date();
d.myMet();
var monthname = d.myProp;

자신의 손으로 테스트해 보세요

문법

Date.prototype.name = value

기술 세부 사항

JavaScript 버전: ECMAScript 1

브라우저 지원

속성 크롬 IE 파이어폭스 사파리 오페라
prototype 지원 지원 지원 지원 지원

관련 페이지

教程:JavaScript 날짜

教程:JavaScript 날짜 형식

教程:JavaScript 객체 생성자