반응형

Global 객체

    new Global() 불가능
    Global.메서드(); 불가능

 

    글로벌 객체는 글로벌 변수나 글로벌 함수를 관리하기 위한 자바스크립트에서 자동으로 생성하는 객체
    단순 편의적인 객체이다.

    글로벌 변수/글로벌 함수란 함수 내에 속하지 않는 탑레벨의 변수나 함수를 의미함.
    디폴트로 지원하는 변수/함수를 활용함(사용자 정의도 가능)

 

    사용방법

        변수명
        함수명(인수, …)

글로벌 객체는 개발에 유용한 기능이 포함되어 있음

특수 값
    NaN : 수치가 아닌 값
    Infinity : 무한대 값
    undefined : 정의되지 않은 값

 

체크
    isFinite(num) : 유한한 값인지 판별
    isNaN(num) : 수치 값인지 판별

 

변환
    Boolean(val) : 논리형으로 변환(실체는 Boolean객체이다)
    Number(val) : 수치형으로 변환(실체는 Number객체이다.)
    String(val) : 문자열 형으로 변환(실체는 String객체이다.)
    parseFloat(str) : 소수점이 있는 형태의 문자열을 수치 형태로 변환
    parseInt(str) : 정수 형태의 문자열을 수치 형태로 변환

 

인코딩
    escape(str) : 문자열을 이스케이프 함
    unescape(str) : 이스케이프 문자열을 원래 값으로 변환
    encodeURI(str) : URI인코딩
    decodeURI(str) : URL디코딩
    encodeURIComponent(str) : URI인코딩
    decodeURIComponent(str) : URL디코딩

 

해석
    eval(exp) : 식이나 값을 평가한다.


자바스크립트는 자료형에 관대하나 변환 시 문제가 발생되는 경우를 알아둘 필요가 있음
자바스크립트에서 +, - 연산의 경우 차이점이 있는 부분 확인

 

parsetest.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Global객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
var n = '123xxx';
document.writeln(Number(n));
document.writeln(parseFloat(n));
document.writeln(parseInt(n));
 
var d = new Date();
document.writeln(Number(d));
document.writeln(parseFloat(d));
document.writeln(parseInt(d));
 
var h = '0777';
document.writeln(Number(h));
document.writeln(parseFloat(h));
document.writeln(parseInt(h));
 
var e = '1.01e+2';
document.writeln(Number(e));
document.writeln(parseFloat(e));
document.writeln(parseInt(e));
</script>
</pre>
</body>
</html>
cs

쿼리정보 이스케이프하기

    브라우저에서 서버로 요청하는 쿼리 문자열에서 ?뒤에 문자열은 =, %, 공백, 멀티바이트문자 등은 사용 불가

    이를 해결하기 위해 %xx 형식으로 변환할 필요가 있는데 이를 URI인코딩이라 한다.

 

encodeURItest.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Global객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
var str = '!"#$%&()+-*/@~_|;:,.';
document.writeln(encodeURI(str));
document.writeln(encodeURIComponent(str));
</script>
</pre>
</body>
</html>
cs

 

결과

    !%22#$%25&()+-*/@~_%7C;:,.
    !%22%23%24%25%26()%2B-*%2F%40~_%7C%3B%3A%2C.

 

encodeURI() 와 encodeURIComponent() 두 함수의 차이점만 확인

 

비슷한 함수로 escape 있는데 이는 플랫폼이나 브라우저에 따라 결과가 다를 있느므로 하위 호환성을 유지할 경우나 외의 특별히 필요한 경우가 아니면 사용을 권장하지 않는다.

이스케이프 문자열은 decode메서드로 escape되기 원래 문자열로 변환할 있다.

 

동적으로 생성한 스크립트 실행하기

    eval함수

 

evaltest.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Global객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
var str = 'window.alert("eval함수")';
eval(str);
</script>
</pre>
</body>
</html>
cs

 

주의(남용하는 것을 자제)

  • 임의의 스크립트가 동작될 있으므로 보안성에 낮아짐
  • 일반적인 코드 실행보다 처리 속도가 느림

eval()메서드는 JSON데이터를 해석하는 용도로만 사용하는 것을 권장함

반응형

'교육자료 > Javascript' 카테고리의 다른 글

15-2. 함수 사용 시 주의  (0) 2019.05.09
15-1. 자바스크립트 함수  (0) 2019.05.09
13. Object 내장객체  (0) 2019.05.09
12. RegExp 객체(정규 표현식)  (0) 2019.05.09
11. Date 내장객체  (0) 2019.05.09

+ Recent posts