익명 함수
익명함수는 변수에 함수를 설정하는 방법입니다.
익명함수
샘플
//선언적 함수
function func1(){
document.write("함수가 실행되었습니다.");
}
func1();
//익명함수
let func2 = function(){
document.write("함수가 실행되었습니다.");
}
func2();샘플2
배경색 변경하기 샘플
Last updated
익명함수는 변수에 함수를 설정하는 방법입니다.
//선언적 함수
function func1(){
document.write("함수가 실행되었습니다.");
}
func1();
//익명함수
let func2 = function(){
document.write("함수가 실행되었습니다.");
}
func2();Last updated
func2(); // 호이스팅방식으로 함수 호출이 먼저나와도 실행이 됨
function func2(){
document.write("함수가 실행되었습니다.")
}
let func3 = function(){
document.write("함수가 실행되었습니다.");
}
func3();<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>javascript38</title>
<script>
let color = ["white", "yellow", "aqua", "purple"];
let i = 0;
function changeColor(){
i++;
if( i >= color.length ){
i = 0;
}
let bodyTag = document.getElementById("theBody");
bodyTag.style.backgroundColor = color[i];
console.log("i : " + i)
console.log("color[i] : " + color[i]);
}
</script>
<!-- <style>
#theBody {
background-color: khaki;
}
</style> -->
</head>
<body id="theBody">
<button onclick="changeColor();">배경색 바꾸기</button>
</body>
</html>