본문으로 바로가기
javascript class생성 및 member filed에 값 binding 그리고 member 출력

1.Person이라는 class 생성
2.person 클래스를 출력하는 function
3.경록김, 경록문으로 Person object 2개 생성
4.for in을 이용해 생성한 object의 member 출력

Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function Person(){
            var name;
            var age;
            var addr;
            var disp;
        }
 
        function printPerson(person){
            document.write("-------------------" + "<br/>");
            document.write("name : "+person.name + "<br/>");
            document.write("age : "+person.age + "<br/>");
            document.write("addr : "+person.addr + "<br/>");
        }
    </script>
</head>
<body>
    <script type="text/javascript">
        var kyeongrok_mun = new Person();
        console.log(kyeongrok_mun);  //이때는 아무 값도 보이지 않는다.
        
        kyeongrok_mun.name ="kyeongrok_mun";
        kyeongrok_mun.age =16;
        kyeongrok_mun.addr = "은평구";
 
        printPerson(kyeongrok_mun);
 
        var kyeongrok_kim = new Person();
        console.log(kyeongrok_kim);
        
        kyeongrok_kim.name ="kyeongrok_kim";
        kyeongrok_kim.age =30;
        kyeongrok_kim.addr = "관악구";
 
        console.log(kyeongrok_kim);
 
        document.write("------------------"+"<br/>");
        for(member in kyeongrok_kim){
            document.write(member+"<br/>");
        }
 
    </script>
 
</body>
</html>
cs


result


end.






댓글을 달아 주세요