JavaScript

[Date] JS - prototype 속성(날짜) - 사용자정의 속성/메서드 추가 (날짜객체경우 = 프로토타입)

prototype 예제 - 날짜객체 경우

 

<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

Date.prototype.monthToString = function() {

  if (this.getMonth() == 0){this.monthName = "1월"};

  if (this.getMonth() == 1){this.monthName = "2월"};

  if (this.getMonth() == 2){this.monthName = "3월"};

  if (this.getMonth() == 3){this.monthName = "4월"};

  if (this.getMonth() == 4){this.monthName = "5월"};

  if (this.getMonth() == 5){this.monthName = "6월"};

  if (this.getMonth() == 6){this.monthName = "7월"};

  if (this.getMonth() == 7){this.monthName = "8월"};

  if (this.getMonth() == 8){this.monthName = "9월"};

  if (this.getMonth() == 9){this.monthName = "10월"};

  if (this.getMonth() == 10){this.monthName = "11월"};

  if (this.getMonth() == 11){this.monthName = "12월"};

};


function homzzang() {

  var d = new Date();

  d.monthToString();

  document.getElementById("demo").innerHTML = d.monthName;

}

</script>

 

결과보기

결과값: August

※ monthToString 메서드와 monthName 속성은 사용자 정의값임.

 


분류 제목
Statement JS - 구문 (Statements)
Statement JS - break 구문 ★ (= break문 = 브레이크문)
Statement JS - continue 구문 ★ (= continue문 = 컨티뉴문)
Statement JS - debugger 구문 (= debugger문 = 디버거문)
Statement JS - do ... while 구문 (= 두와일구문 = 두와일문 = 두와일반복문)
Statement JS - for 구문 (= for문 = 포구문 = 포문 = 포반복문)
Statement JS - for...in 구문 (= 포인구문 = 포인문 = 포인반복문)
Statement JS - function 구문 (= 함수구문)
Statement JS - if ... else if ... else ... 구문 (= if문 = 이프문 = 이프조건문)
Statement JS - return 구문 ★★ (= return문 = return구문 = 리턴문 = 반환문 = 리턴구문)…
Statement JS - switch 조건문 ★ (= switch문 = 스위치문)
Statement JS - throw 구문 (= 쓰로우문 = 쓰로우구문)
Statement JS - try ... catch ... finally 구문 (= 트라이캐치파이널구문)
Statement JS - var 구문 (= 변수정의)
Statement JS - while 구문 (= 와일문 = 와일반복문)
12/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인