Good programmers HTML5 large front-end learning function to share the articles, the code is written in a function call that code can be avoided in the case of non-essential, which means that we can let go execute a piece of code in a particular case.
function keyword: The keyword indicates to declare a function.
How to perform a function ()
function m1(){
// xxxxxxxxxxxxx
}
for(var i=0;i<10;i++){
m1 ();
}
Just mentioned, the significance of the function is to run a function in a particular case, then what is the specific cases that?
JavaScript is an event -driven language!
Event: the user's behavior.
onclick、ondblclick、onfocus、onblur
Such as clicking a button; pop 123;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<button type="button"onclick = btn()>弹框</button>
</body>
<script type="text/javascript">
function btn (){
alert(123);
}
</script>
</html>
Function parameters
function m1(v1, v2, v3...){
// xxxxxxxxxxxxx
}
The return keyword
With return function values
Case:
Date are custom function parameters to determine whether the correct date
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
function date(year,month,day){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:if(day<0||day>31){
return 'incorrect date';
}
break;
case 2:if(year % 4 == 0 || year% 400 == 0 && year % 100 != 0){
if(day>29||day<0){
return 'incorrect date';
}
}else{
if(day>28||day<0){
return 'incorrect date';
}
}
break;
default:if(day>30||day<0){
return 'incorrect date';
}
}
if(year>2050||year<0){
return 'wrong year';
}else if(month>12||month<0){
return 'bad month';
}else{
return 'correct date'
}
};
alert(date(2000,2,30))
</script>
</head>
<body>
</body>
</html>
��h�rَ�i7
Reproduced in: https: //www.jianshu.com/p/02ffe8c9fd2a