for in문과 객체 속성 출력-02.html

CODEDRAGON Development/JavaScript, jQuery, ...

반응형

for in문과 객체 속성 출력

   

for in문을 상요하면 객체에 포함된 모든 프로퍼티에 대해 푸프를 수행할 수 있습니다.

   

객체의 속성을 반복문을 이용해 호출

   

   

   

//product.key: key라는 이름의 key를 호출하는 것이기 때문에 에러

//for in 반복문은 product.key는 인정되지 않음

for(var key in product){

output += '*' + key + ' : '+ product.key + '\n';

}

   


   

   

   

전체 소스

<html>

<head>

<meta charset="UTF-8">

<title>객체의 속성을 반복문을 이용해 호출</title>

<script type="text/javascript">

//객체 생성

var product = {

//속성(key:value)

name:'eclipse',

price:'3,000,000',

languge:'engish',

supportOS:'Linux',

subscription:true

};

//출력용 변수

var output ='';

//for in 반복문

for(var key in product){

//product.key: key라는 이름의 key 호출하는 것이기 때문에 에러

//for in 반복문은 product.key 인정되지 않음

//output += '*' + key + ' : '+ product.key + '\n';

output += '*' + key + ' : '+ product[key] + '\n';

}

alert(output);

</script>

</head>

<body>

</body>

</html>

   


   

   

   

반응형