<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
// 함수정의
function prn() {
document.writeln('Hello Javascript!!<br>');
} // prn()의 끝
function mySum(a, b, c) {
document.write(a+'+'+b+'+'+c+'='+(a+b+c)+'<br>');
}
function myAbs(num) {
if (num < 0) {
num = -num;
}
return num;
// return num<0 ? -num : num;
}
function myMax(a, b) {
var result;
if (a>b) {
result = a;
} else {
result = b;
}
return result;
}
// 함수호출
prn(); // 출력 "Hello Javascript!!"
mySum(10, 20, 30); // 값을 전달 받아서 출력 "10+20+30=60"
// 값을 전달받아서 절대값 구하고 값 return
document.write('절대값: ' + myAbs(-5) + '<br>');
// 값을 전달받아서 최대값 구하고 값 return
document.write('최대값: ' + myMax(10, 20) + '<br>');
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function func1 () {
var num1 = 5;
document.write('num1 = '+num1+'<br>');
func2(); // 함수는 메모리에 먼저 올라오므로 함수 정의 전에 호출문을 써도 된다.
function func2() {
document.write('func2() <br>');
} // func2()의 끝
var name = "홍길동";
var func3 = function () {
document.write('익명함수() 호출<br>');
}; // 실행문이므로 ;을 써줘야된다.
func3();
} // func1()의 끝
func1();
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function func1(a) {
a(); // 함수를 매개변수로 전달받아 호출함
}
function func2() {
alert('함수호출');
}
func1(func2); // 함수를 매개변수로 전달
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function func1(a) {
a();
}
var b = function () { // 함수를 변수에 대입
alert('함수호출');
};
//func1(b);
</script>
</head>
<body>
<input type="button" value="버튼" onclick="func1(b)">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function func1(a) {
a();
}
// var b = function () {
// alert('함수호출');
// };
var num = 5;
func1(function () { // 매개변수 자리에 바로 함수를 넣을 수도 있다.
alert('함수호출');
}); // func1() 함수호출문
function func2() {
alert('메시지');
}
</script>
</head>
<body>
<input type="button" value="버튼" onclick="func2()">
</body>
</html>
'IT > JavaScript' 카테고리의 다른 글
javascript - setTimeout()으로 웹 페이지 자동 연결 (0) | 2018.08.31 |
---|---|
javascript - form submit 제한의 여러가지 방법 (0) | 2018.08.30 |
javascript - 이벤트 기초 및 활용법, preventDefault, stopPropagation, form, radio, checkbox (0) | 2018.08.27 |
javascript - document.getElementById, document.write(), prompt, eval, 조건문, 반복문, 함수 (0) | 2018.08.26 |