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 속성은 사용자 정의값임.

 

방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
Basic JS - Home (JS입문) + Javascript Framework (프레임워크) 종류
Basic JS - Intro (JS소개)
Basic JS - Where To (JS위치) - JS구문 / JS코드위치 / JS사용법 ※ JS외부링크 주의사항
Basic JS - Output (JS출력= JS쓰기) ★★★★★
Basic JS - Syntax (JS구문) ★
Basic JS - Statement (JS구문= JS명령문)
Basic JS - Comment (JS주석)
Basic JS - Variable (JS변수) ★★★★★
Basic JS - Operator (연산자) - JS연산자 ★★★★★
Basic JS - Data Type - 데이터유형 ★★★★★ (= 데이터형식 = 데이터타입 = 데이터종류 = 자료형…
Basic JS - Function - JS함수 ★★★★★ ※ 일반함수 특징 2
Basic JS - Object - JS객체 ★★★★★
Basic JS - Scope - JS유효범위 (= JS접근범위 = 변수 종류) ★★★★★★★★★★
Basic JS - Event - JS이벤트 (= JS코드실행방법) ★★★★★
Basic JS - Strings - JS문자열
1/89
목록
 홈  PC버전 로그인 일본어
웹디자인언어
서버관리언어
고급코딩언어
그누보드 2
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱 PC버전 로그인