JavaScript

코스 전체목록

닫기

사용자 정의 객체

본 수업은 폐지 예정입니다. 자바스크립트 언어 수업웹브라우저 자바스크립트로 수업이 리뉴얼 되었기 때문에 이것을 이용해주세요.

사용자 정의 객체란?

사용자가 직접 만든 객체를 의미하고 아래와 같은 방법으로 정의 할 수 있다.

// new 연산자와 object() 생성자 함수를 이용해서 객체생성
new Object();
// 객체 리터럴을 이용해서 객체 생성
{속성의 이름: 속성의 값, 속성의 이름:속성의 값....}

예제

example1.html - 객체를 사용하지 않은 프로그래밍 스타일 (jsfiddle, github)

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
		<script type="text/javascript">
			// 객체를 사용하지 않은 코딩 스타일
			var age = 30;
			var job = '프로그래머';
			function introduce() {
				alert('저는' + age + '세의 ' + job + '입니다');
			}

			introduce();
		</script>
	</head>
	<body></body>
</html>

example2.html - 객체를 사용한 코딩 스타일 (jsfiddle, github)

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
		<script type="text/javascript">
			// 객체를 사용한 코딩 스타일
			var person = new Object();
			person.age = 30;
			person.job = '프로그래머';
			person.introduce = function() {
				alert('저는 ' + this.age + '세의 ' + this.job + '입니다');
			}
			person.work = function() {
				alert('coding');
			}
			alert(person.age);
			person.introduce();
		</script>
	</head>
	<body></body>
</html>

example3.html - 객체 리터럴을 이용한 객체의 정의 (jsfiddle, github)

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
		<!-- 객체 리터럴 -->
		<script type="text/javascript">
			var person = {
				age : 30,
				job : '개발자',
				introduce : function() {
					alert('저는 ' + this.age + '세의 ' + this.job + '입니다');
				},
				work : function() {
					alert('coding');
				}
			}
			alert(person.age);
			person.introduce();
		</script>
	</head>
	<body></body>
</html>

객체지향이란?

객체에 연관되어 있는 속성과 메소드를 포함시켜서 다음의 목적을 달성하기 위한 프로그래밍 패러다임으로 영어로 Object oriented programming라고 하고, 줄여서 OOP라고 부른다. 

  • 캡슐화
  • 추상화
  • 다형성
  • 상속
  • 인스턴스
  • 메시지전달

참고

댓글

댓글 본문
버전 관리
egoing
현재 버전
선택 버전
graphittie 자세히 보기