본문 바로가기

IT/Javascript

자바스크립트의 Object Model

자바스크립트의 Object Model


웹브라우저의 구성요소들은 하나하나 객체화 되어있다.

자바스크립트는 이 객체를 제어해 웹브라우저를 제어하게 된다.

BOM(Browser Object Model)과 DOM(Document Object Model)은 이 구조를 구성하고 있는 가장 큰 틀의 분류라고 생각하면 된다.


이관계는 아래와 같이 나타낼 수 있다.

window는 전역객체로 보면된다.

그러므로, window.document 대신, document를 써도 객체에 접근이 가능하다.






JavaScript Core

JavaScript 언어 자체에 정의되어 있는 객체들.

String

문자열 객체. 문자를 제어하는 다양한 메소드와 속성을 가지고 있다.

1, charAt

2. charCodeAt

3.     concat

4.     indexOf

5.     lastIndexOf

6.     localeCompare

7.     match

8.     replace

9.     search

10.  slice

11.  split

12.  substr

13.  substring

14.  toLowerCase

15.  toUpperCase

16.  valueOf

 

Array (배열)

연관되어 있는 복수의 값을 하나의 컨테이너로 관리

1.     concat

2.     join

3.     pop

4.     push

5.     reverse

6.     shift

7.     slice

8.     sort

9.     splice

10.  toString

11.  unshift

12.  valueOf

 

Math

수학을 위한 객체

1.     abs

2.     acos

3.     asin

4.     atan

5.     atan2

6.     ceil

7.     cos

8.     exp

9.     floor

10.  log

11.  max

12.  min

13.  pow

14.  random

15.  round

16.  sin

17.  sqrt

18.  tan

 

Number

객체

1.     toExponential

2.     toFixed

3.     toPrecision

4.     toString

5.     valueOf

Date

날짜와 시간에 관한 객체

1.     getDate

2.     getDay

3.     getFullYear

4.     getHours

5.     getMilliseconds

6.     getMinutes

7.     getMonth

8.     getSeconds

9.     getTime

10.  getTimezoneOffset

11.  getUTCDate

12.  getUTCDay

13.  getUTCFullYear

14.  getUTCHours

15.  getUTCMilliseconds

16.  getUTCMinutes

17.  getUTCMonth

18.  getUTCSeconds

19.  getYear

20.  parse

21.  setDate

22.  setFullYear

23.  setHours

24.  setMilliseconds

25.  setMinutes

26.  setMonth

27.  setSeconds

28.  setTime

29.  setUTCDate

30.  setUTCFullYear

31.  setUTCHours

32.  setUTCMilliseconds

33.  setUTCMinutes

34.  setUTCMonth

35.  setUTCSeconds

36.  setYear

37.  toDateString

38.  toGMTString

39.  toLocaleDateString

40.  toLocaleTimeString

41.  toLocaleString

42.  toString

43.  toTimeString

44.  toUTCString

45.  UTC

46.  valueOf

Boolean

boolean value 객체화(wrapper) 시킴

1.     toString

2.     valueOf

RegExp

정규표현식(Regular Expression) 사용하기 위한 객체를 생성

1.     exec

2.     test

Global

특정한 객체에 속하지 않는 함수와 속성들

1.     decodeURI

2.     decodeURIComponent

3.     encodeURI

4.     encodeURIComponent

5.     escape

6.     eval

7.     isFinite

8.     isNaN

9.     Number

10.  parseFloat

11.  parseInt

12.  String

13.  unescape



BOM

Browser Object Model. 웹페이지의 내용을 제외한 브라우저의 각종 요소들을 객체화시킨 것이다. 전역객체 Window의 프로퍼티에 속한 객체들이 이에 속한다.

웹브라우저를 운영하기 위한 객체를 조작합니다새창을 열거나, 또는 현재창의 url을 가져오거나

브라우저의 버전, 모델을 알 수 있음.



<!DOCTYPE html>
<html>
<body>
<input type="button" onclick="alert(window.location)" value="alert(window.location)" />
<input type="button" onclick="window.open('bom.html')" value="window.open('bom.html')" />
</body>
</html>



alert

경고창이라 부른다. 사용자에게 메시지를 주거나, 디버깅 등의 용도로 사용된다.
<!DOCTYPE html>
<html>
<body>
<input type="button" value="alert" onclick="alert('hello world');" />
</body>
</html>



confrim

확인을 누르면 true, 취소를 누르면 false를 리턴한다.


<!DOCTYPE html>

<html>
<body>
<input type="button" value="confirm" onclick="func_confirm()" />
<script>
function func_confirm(){
if(confirm('ok?')){
alert('ok');
} else {
alert('cancel');
}
}
</script>
</body>

</html>


아래와 같이 확인 / 취소 분기되어 알림창이 뜬다.




prompt

사용자로 하여금 입력값을 받을 수 있다.

<!DOCTYPE html>
<html>
<body>
<input type="button" value="prompt" onclick="func_prompt()" />
<script>
function func_prompt(){
//문자열이 같으면 알림창을 뜬다.
if(prompt('id?') === 'lotty'){
alert('welcome');
} else {
alert('fail');
}
}
</script>
</body>
</html>


아래와 같이 사용자로 하여금 입력값을 받아야 하는 것을 확인할 수 있다.





DOM

Document Object Model. 웹페이지의 내용을 제어한다. window의 프로퍼티인 document 프로퍼터에 할당된 Document 객체가 이러한 작업을 담당한다.
일례로, <body>, <img> 등과 같은 태그가 이에 속한다.

Document 객체의 프로퍼티는 문서 내의 주요 엘리먼트에 접근할 수 있는 객체를 제공한다.


또한 특정한 엘러먼트의 객체를 획득할 수 있는 메소드도 제공한다. 


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
// body 객체
console.log(document.getElementsByTagName('body')[0]);
// 이미지 객체들의 리스트
console.log(document.getElementsByTagName('body'));
</script>
</body>

</html>


크롬 개발자도구에서 확인하면, 아래와 같이 엘리먼트 요소들을 가져온 것을 확인할 수 있다.


출처: 생활코딩