1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<script>
//빈 객체선언
var student = {};
//객체 속성 추가
student.이름 = '멍개';
student.취미 = '운동';
student.특기 = '프로그래밍';
student.장래희망 = '개발자';
//메서드 추가
student.toString = function () {
var output = '';
for (var key in this) {
if (key != 'toString')
output += key + '\t' + this[key] + '\n';
}
return output;
};
alert(student.toString());
</script>
| cs |
위 코드는 객체를 동적으로 추가해주는 코드이다.
속성을 동적으로 추가를 시킨다.
메서드를 추가하여 메서드에서 output에 속성들을 저장을 한다.
저장을할때 key에 메서드가 들어가면 output에 속성값을 넣지 않도록 if를 넣어주었다.

동적으로 생성된 객체의 속성, 속성값이다.
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
|
<script>
//빈 객체선언
var student = {};
//객체 속성 추가
student.이름='멍개';
student.취미='운동';
student.특기='프로그래밍';
student.장래희망='개발자';
student.toString = function () {
var output ='';
for (var key in this) {
if (key != 'toString')
output += key + ' : \t' +this[key] + '\n';
}
return output;
};
alert(student);
//속성 제거
delete (student.장래희망);
//속성 제거 후 출력
alert(student);
</script>
| cs |
delete를 이용하여 속성을 제거할 수 있다.

댓글
댓글 쓰기