1. declaration
<title>객체</title>
</head>
<body>
<script type="text/javascript">
const product = {
제품명:'휴대폰',
제품번호:'A1001',
기능:'멀티윈도우',
원산지:'대한민국',
가격:1000,
업데이트지원:true,
};
document.write(product.제품명 + '<br>');
document.write(product.원산지 + '<br>')
document.write(product['가격'] + '<br>');
document.write(product['업데이트지원']);
</script>
</body>
</html>
2. loop
<title>반복문을 이용해서 객체의 속성 읽기</title>
</head>
<body>
<script type="text/javascript">
const product = {
name:'eclipse',
price:'10,000',
language:'한국어',
supportOS:'win10',
subscription:true,
};
for(let key in product){
document.write(key + ':' + product[key] + '<br>');
}
</script>
</body>
</html>
3. method
<title>객체의 속성과 메서드 사용</title>
</head>
<body>
<script type="text/javascript">
let name = '유재석';
const person = {
name:'홍길동',
eat:function(food){
let name = '강호동';
document.write(this.name + '이 ' + food + '을 먹습니다.<br>');
document.write(name + '이 ' + food + '을 먹습니다.');
}
};
person.eat('밥');
</script>
</body>
</html>
[실행결과]
홍길동이 밥을 먹습니다.
강호동이 밥을 먹습니다.
4. insert
<title>빈 객체에 속성 추가</title>
</head>
<body>
<script type="text/javascript">
const student ={};
student.이름 = '홍길동';
student.취미 = '악기';
student.특기 = '프로그래밍';
student.장래희망 = '프로그래머';
document.write('특기' in student);
document.write('<br>');
document.write(student.특기 + '<br>');
document.write('-------------<br>');
student.play = function(){
document.write('피아노를 연주하다');
};
for(let key in student){
document.write(key + ' : ' + student[key] + '<br>');
}
</script>
</body>
</html>
[실행결과]
true
프로그래밍
-------------
이름 : 홍길동
취미 : 악기
특기 : 프로그래밍
장래희망 : 프로그래머
play : function(){ document.write('피아노를 연주하다'); }
5. toString
<title>toString() 메서드 사용</title>
</head>
<body>
<script type="text/javascript">
const student = {};
document.write(student + '<br>');
document.write(student.toString() + '<br>');
document.write('--------------------<br>');
student.이름 = '홍길동';
student.취미 = '악기';
student.특기 = '프로그래밍';
student.장래희망 = '프로그래머';
for(let key in student){
document.write(key + ' : ' + student[key] + '<br>');
}
document.write('--------------------<br>');
student.toString = function(){
let msg = '';
for(let key in this){
if(key != 'toString'){
msg += key + ' : ' + this[key] + '<br>';
}
}
return msg;
};
document.write(student.toString() + '<br>');
document.write('--------------------<br>');
document.write(student);
</script>
</body>
</html>
6. delete
<title>속성 제거</title>
</head>
<body>
<script type="text/javascript">
const product = {};
product.이름 = '건조기';
product.가격 = 20000;
product.색상 = '흰색';
product.제조사 = 'LG';
product.toString = function(){
let output = '';
for(let key in this){
if(key != 'toString'){
output += key + ' : ' + this[key] + '<br>';
}
}
return output;
};
document.write(product);
document.write('-----------------<br>');
delete product.제조사;
document.write(product);
</script>
</body>
</html>