Window 객체

브라우저에서의 자바스크립트 전역객체 window 객체

Window 객체

  • 모든 객체가 소속된 객체이고, 전역객체이면서, 창이나 프레임을 의미함

  • winodw.document, window.navigator 등 모든 객체가 소속됨(전역 객체로 window를 생략해도 같은 기능을 수행 => window.alert('')= alert(''))

  • 웹 브라우져 환경에서 전역변수를 선언한다는 것은 window 객체에 프로퍼티를 만드는 것과 같음(전역변수 a = window.a)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
</head>
<body>
  <script>
    // 아래 2개는 같은 기능을 함
    window.alert('alert');
    alert('alert');
    // 전역객체 선언
    var a = 5;
    document.write(a);
    document.write('<br />');
    // window로 전역객체 접근
    document.write(window.a);
  </script>
</body>
</html>

Last updated

Was this helpful?