<HTML>
<HEAD>
<TITLE> :::: 자바스크립트 객체를 객체 속성으로 사용하기 :::: </TITLE>
<script language="JavaScript">
/** 과목이라는 객체를 구현하도록 함수로써 설계하였다. */
function subject(kor, eng, math)
{
this.kor = kor; //국어
this.eng = eng; //영어
this.math = math; //수학
}//subject///////////////////////////////
/** 위의 과목이라는 객체에 출력이라는 행동.. 즉 메소드를
* 설정하기 위해서 함수를 통해서 메소드를 설계합니다. */
function display()
{
document.write("이름 : " + this.name + "<br>");
document.write("국어 = " + this.subject.kor + "<br>");
document.write("영어 = " + this.subject.eng + "<br>");
document.write("수학 = " + this.subject.math + "<p>");
}//display()=========================================
/** 학생이라는 객체를 구현하도록 함수로써 설계하였다. */
function student(my_name, subject)
{
this.name = my_name; //이름
this.subject = subject; //성적
this.dsp = display; //메소드 설정
}//my_user///////////////////////////////
</script>
</HEAD>
<BODY>
<script language="JavaScript">
var subject01 = new subject(100, 100, 100); //과목객체 생성
var subject02 = new subject(80, 90, 88);
var young_ho = new student("영호", subject01);
var min_su = new student("민수", subject02);
document.write("<h2>");
young_ho.dsp(); //메소드 호출
min_su.dsp();
document.write("</h2>");
</script>
</BODY>
</HTML>
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
위의 코드는 객체 안에 객체가 들어간 형태로 짜여진 코드이다. 즉 객체안에 속성으로 객체가
들어간 형태이다. 자바와 달리 자바스크립트에서는 클래스가 없기 때문에 이처럼
함수를 이용하여 객체를 생성하고 매개변수를 통해서 객체안에 객체를 대입하는 형태를 사용하였다.
그리고 상위객체는 하위객체를 받아들일 수 있도록 속성을 부여한것을 볼 수 있다.
그리고 하위객체와 같은 동급의 선상에 위치하고 있는 메소드는 하위객체에 있는 속성을
출력하기 위해서 this.하위객체.속성명 으로 하여 접근한 것을 볼 수 있다.
그리고 상위객체의 속성은 해당 메소드입장에서 보면 전역변수와 같다.
때문에 this.속성명 으로 접근한 것을 볼 수 있다. 여기서 this란 자기자신 즉
자기가 속한 객체를 말한다.
위의 코드를 자바식으로 재조합하여 작성하면 다음과 같다.
------------------------------------------------------------------------------------------
function student(my_name, subject)
{
this.name = my_name; //이름
function subject(kor, eng, math)
{
this.kor = kor; //국어
this.eng = eng; //영어
this.math = math; //수학
}//subject///////////////////////////////
function display()
{
document.write("이름 : " + this.name + "<br>");
document.write("국어 = " + this.subject.kor + "<br>");
document.write("영어 = " + this.subject.eng + "<br>");
document.write("수학 = " + this.subject.math + "<p>");
}//display()=========================================
}//my_user///////////////////////////////
-------------------------------------------------------------------------------------
자바식으로 고치면 위와 같겠다. 위의것을 자바 문법에 맞게 고치면 다음과 같다.
-------------------------------------------------------------------------------------
class Student
{
String name;
Subject subject;
public Student(){}//기본생성자
public Student(String name, Subject subject)
{
this.name = name;
this.subject = subject;
}//인자생성자
public static class Subject
{
int kor; //국어
int eng; //영어
int math; //수학
public Subject(){} //기본생성자
public Subject(int kor, int eng, int math)
{
this.kor = kor;
this.eng = eng;
this.math = math;
}//이너클래스의 인자생성자
}//subject///////////////////////////////
public void display()
{
System.out.println("이름 : " + this.name);
System.out.println("국어 = " + this.subject.kor);
System.out.println("영어 = " + this.subject.eng);
System.out.println("수학 = " + this.subject.math+"\n");
}//display()=========================================
public static void main(String args[])
{
Student.Subject subject01 = new Student.Subject(100, 100, 100);
Student.Subject subject02 = new Student.Subject(80, 90, 88);
Student young_ho = new Student("영호", subject01);
Student min_su = new Student("민수", subject02);
young_ho.display();
min_su.display();
}
}//my_user///////////////////////////////
----------------------------------------------------------------------------------------
그리고 결과화면도 확인하면 다음과 같다.
----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
이처럼 자바와 자바스크립트는 oop 즉 객체지향의 언어이기 때문에 모두 객체생성이 가능하다.
단, 자바스크립트에는 클래스라는것이 없으므로 함수를 통해서 객체를 생성해야 한다는점을
잊지말자. 그리고 코드가 길어질수록 자바스크립트는 코드분석이 어려울 수 있다.
(자바는 들여쓰기로 구분이 어느정도 쉽지만 자바스크립트는 상대적으로 그렇지 않기 때문이다.)
그러니 주석을 잘 달아놓는것이 도움이 될 것이다.
'JavaScript' 카테고리의 다른 글
자바스크립트 배열사용하기 02 (Array객체 사용하기) (0) | 2013.08.04 |
---|---|
자바스크립트 배열사용하기 01 (배열함수를 만들어 사용하기) (0) | 2013.08.04 |
자바스크립트 객체생성 및 메소드 설정하기 (0) | 2013.08.04 |
자바스크립트 객체생성하기(객체에 속성부여하기) (0) | 2013.08.03 |
자바스크립트 재귀호출함수 (0) | 2013.08.03 |