객체 생성

우선 객체를 생성할 변수를 선언 후 new라는 명령어를 사용하고 객체 속성을 선언한 함수명을 적어 줍니다.

그렇다면 이렇게 됩니다.

r8 = new makeCar();

그리고 자동차의 이름 등을 값으로 넣어 줍니다. this.name = "r8" 이런식으로요.

예제를 통해 알아 봅시다.

r8 = new makeCar();
 r8.name="R8";
 r8.company = "AUDI";
 r8.color = "white";
 r8.numPeople = 2;
 r8.meth(); //메소드 호출


그럼 실전 예제를 통해서 테스트 해봅시다.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>자바스크립트</title>
<script>
function makeCar(){
    this.name;
    this.company;
    this.color;
    this.numPeople;
    this.meth = showCar;
}
function showCar(){
    document.write("자동차 이름 : "+this.name+"<br />");
    document.write("자동차 제조사 : "+this.company+"<br />");
    document.write("자동차 탑승인원 : "+this.numPeople +"<br />");
    document.write("차량 색상 : "+ this.color +"<br />");
}
r8 = new makeCar();
r8.name="R8";
r8.company = "AUDI";
r8.color = "white";
r8.numPeople = 2;
r8.meth();
</script>
</head>
<body>
</body>
</html>


 

r8이 아닌 다른 객체를 생성 한다면 같은 위와 같은 방법으로 다른 객체를 생성 하시면 됩니다.

a8 = new makeCar();
 a8.name="A8";
 a8.company = "AUDI";
 a8.color = "Red";
 a8.numPeople = 4;
 a8.meth();


위의 소스를 덧붙여서 시도 해 보면 또 하나의 객체 가 생성되어 출력 메소드를 통하여 정보를 보여 줍니다.

 

출처 : http://www.everdevel.com/

 

 

 

다른 일을 하면서도 투잡(알바/부업)으로 월급 만큰 수익이 됩니다.

아래 접속하셔서 상담받아 보실 수 있습니다. (믿음의 재택부업회사)

 

블로그 이미지

itworldkorea

IT korea가 세상(world)을 변화시킨다.

,