1)변수와 함수의 차이
변수는 데이터 1개만 보관이 가능
함수는 자바스크립트 코드를 저장
2)기본 함수 정의
*기본형
fuction 함수명() {
자바스크립트 코드;
}
*익명함수 버전
참조변수 = function(){
자바스크립트 코드;
}
함수는 메모리에 할당되어 대기하다가 호출이 되어야 실행이 가능하다
호출 하는 법
함수명();
참조변수();
var count = 0;
myFnc();
function myFnc(){ //함수 정의문
count++;
document.write("hello" +count,"<br >");
}
myFnc();
var theFnc=function(){ //참조 변수
count++;
document.write("bye"+count,"< br >");
}
theFnc();
myFnc(); hello 와 count ,한줄 띄기 출력
myFnc(); hello 와 count ,한줄 띄기 출력
myFnc(); bye 와 count ,한줄 띄기 출력
hello1
hello2
bye3
< !DOCTYPE html >
< html lang="en" >
< head >
< meta charset="UTF-8" >
< meta http-equiv="X-UA-Compatible" content="IE=edge" >
< meta name="viewport" content="width=device-width, initial-scale=1.0" >
< title >Document
< script type="text/javascript" >
var color = ["white","yellow","aqua","purple"];
var i =0;
function changeColor(){
i++;
if(i >=color.length){
i=0;
}
var bodyTag= document.getElementById("theBody");
bodyTag.style.backgroundColor = color[i];
}
</script >
< /head >
< body id="theBody " >
< button onclick="changeColor();" >배경색바꾸기< /button >
< /body >
< /html >