공돌공돌 2012. 8. 27. 13:45

 <script type="text/javascript">
        // 객체 선언
        var student = {
            name : '장',
            korean : 92,
            math : 93,
            eng : 94,
            science : 95
        };
        // 출력
        var output = '';
        //with 키워드
        with (student) {
            //output+= '이름: ' + student.이름 + \n';
            //with 키워드를 사용하지 않다면 위와 같이 기술해야 하지만
            //with 키워드를 사용하면 아래와 같이 쉽게 기술할 수 있다
            output += '이름: ' + name + '\n';
            output += '국어: ' + korean + '\n';
            output += '수학: ' + math + '\n';
            output += '영어: ' + eng + '\n';
            output += '과학: ' + science + '\n';
            output += '총점: ' + (korean + math + eng + science);
        }
        alert(output);
    </script>

    With 키워드는 복잡하게 사용해야 하는 코드를 짧게 줄여주는 키워드이다


    일반적으로 객체의 속성을 출력할 대 식별자를 여러번 사용하니 코드가 복잡한데


    with 키워드를 사용하면 객체를 명시할 필요없이 속성을 쉽게 사용 할 수 있다