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

 


분류 제목
Number JS - toPrecision() 메서드 - 숫자를 지정 길이로 변환 (= 투프리시전)
Number JS - toString() 메서드(숫자경우) - 숫자를 문자열타입으로 변경 (= toString메서드 = …
Number JS - valueOf() 메서드 - 객체값 (숫자자체 = valueOf메서드 = 밸류어브)
Operator JS - 연산자 (Operator)
Operator JS - 산술연산자 (Arithmetic Operator)
Operator JS - 할당연산자 (Assignment Operator)
Operator JS - 문자열연산자 (= String Operator = 연결연산자 Concatenation Operato…
Operator JS - 비교연산자 (Comparison Operator)
Operator JS - 논리연산자 (Logical Operator)
Operator JS - 비트연산자 (Bitwise Operator)
Operator JS - typeof 연산자 - 데이터타입 반환/확인/체크 (= typeof연산자 = 타입어브연산자/타입어브…
Operator JS - delete 연산자 (delete Operator = 삭제연산자)
Operator JS - in 연산자 (in Operator = 포함연산자)
Operator JS - instanceof 연산자 (instanceof Operator = 인스턴스연산자)
Operator JS - void 연산자 (= void Operator = 보이드 연산자) + 클릭 배경색 변경
11/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어 1
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 1
웹유틸
회원센터
홈짱닷컴 PC버전 로그인