JavaScript(4)

개미는뚠뚠·2022년 7월 19일
0

JavaScript

목록 보기
4/7
post-thumbnail

Javascript 기초문법(10)_객체지향프로그래밍

객체지향프로그래밍 예시

	function song(title, writer, singer, released){			//함수선언 노래를 예시
      this.title = title;								//this키워드를 통해 받아줌
      this.writer = writer;
      this.singer = singer;
      this.released = released;

      this.getReleasedDay = fuction(){ 	//오브젝트 안에 프로퍼티 외에도 메서드 사용 가능
        return this.released.getDay();	//song의 getDay값을 리턴, 콘솔로 찍으면 결과 값 받음
	
  	  this.getSongInfo = function(){
	  return `제목: ${this.title}, 작곡: ${this.writer}, 가수 ${this.singer}`;
	}									//이런식으로 메서드를 직접 만들어서 출력도 가능
   										//console로 찍으면 제목:, 작곡:, 가수 값 포함해서 return


//사용하기 위해 인스턴스화
	const song1 = new song('love yourself', 'eminem',  'eminem', '1998-07-11');
	const song2 = new song('hater', 'joe',  'joe', '2015-11-2');
					//new키워드를 통해 인스턴스화, new song에 입력한 내용이 들어감 
					//new를 빼면 this는 윈도우 자바스크립트 객체를 전부 받음

	console.log(song1.released.getFullYear()); 	
                    //자바스크립트에서 제공해주는 메서드 활용.
                    //getFullYear()는 song1의 realeased의 value인 2015년도만 출력
					//해당 메서드 외에도 월, 일, 요일(숫자로 나옴) 등 활용할 수 있는 메서드가 많음

0개의 댓글