반응형
return은 도중에 개행을 하지 말 것.
returntest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>함수의 기본</title>
</head>
<body>
<pre>
<script type="text/javascript">
var triangle = function(base, height) {
return
base * height / 2;
};
document.writeln('삼각형의 면적:' + triangle(5,2));
</script>
</pre>
</body>
</html>
|
cs |
함수는 데이터 타입이다.
datatest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>함수의 기본</title>
</head>
<body>
<pre>
<script type="text/javascript">
var triangle = function(base, height) {
return base * height / 2;
};
document.writeln(triangle(5,2));
triangle = 0;
document.writeln(triangle);
</script>
</pre>
</body>
</html>
|
cs |
data1test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>함수의 기본</title>
</head>
<body>
<pre>
<script type="text/javascript">
var triangle = function(base, height) {
return base * height / 2;
};
document.writeln(triangle);
</script>
</pre>
</body>
</html>
|
cs |
function명령은 정적이다.
statictest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>함수의 기본</title>
</head>
<body>
<pre>
<script>
function triangle(base, height) {
return base * height / 2;
}
</script>
<script type="text/javascript">
document.writeln('삼각형의 면적:' + triangle(5,2));
</script>
</pre>
</body>
</html>
|
cs |
함수의 사용보다 정의가 먼저 되어 있어야 동작이 가능하다.
이는 코드를 해석하기 위한 컴파일 시점에 함수 선언을 확인하여 메모리에 등록한다.
따라서 어디서든 함수를 호출 할 수 있다.
만약 함수를 별도로 정의하는 경우 함수를 정의하는 함수 리터럴과 Function생성자는 실행 시점에 판단된다.
(호출하는 코드보다 먼저 정의되어야 함)
static2test.html (개발자 도구로 에러 확인. ractangle 함수를 알지 못한다.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>함수의 기본</title>
</head>
<body>
<pre>
<script type="text/javascript">
document.writeln('삼각형의 면적:' + triangle(5,2));
var triangle = function(base, height) {
return base * height / 2;
};
</script>
</pre>
</body>
</html>
|
cs |
반응형
'교육자료 > Javascript' 카테고리의 다른 글
15-4. argument 객체 (0) | 2019.05.09 |
---|---|
15-3. 변수의 범위 (0) | 2019.05.09 |
15-1. 자바스크립트 함수 (0) | 2019.05.09 |
14. Global 내장객체 (0) | 2019.05.09 |
13. Object 내장객체 (0) | 2019.05.09 |