- 函数声明及调用
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
//函数的声明
function fun01(){
document.write("我在山里");
}
//函数的调用
fun01();
</script>
声明式函数
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
//函数的声明
function fun01(){
document.write("我在山里<br>");
}
//函数的调用
fun01();
//声明式函数
let fun02 = function(){
document.write("你在湖里<br>");
}
fun02();
</script>
箭头函数
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
//函数的声明
function fun01(){
document.write("我在山里<br>");
}
//函数的调用
fun01();
//声明式函数
let fun02 = function(){
document.write("你在湖里<br>");
}
fun02();
// 箭头函数
let fun03 = () => {
document.writeln("爱却在云里<br>");
};
fun03();
</script>
含参函数
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
//带参函数
function fun01(age){
document.write("我的年龄是" + age);
}
//调用
fun01(18);
</script>
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
let fun01 = function (age) {
document.writeln('age >>> ' + age + '<br>');
}
fun01('18');
let fun02 = (gender) => {
document.writeln('gender >>> ' + gender + '<br>');
}
fun02('男');
</script>
有返回值的函数
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
//有返回值的函数
function fun01(age) {
return "我的年龄是>>>" + age;
}
let fun = fun01("18");
document.writeln(fun + '<br>');
</script>
立即执行的函数
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
function fun(){
document.write("我在山里,你在湖里,爱却在云里" + "<br>");
}
fun();
//立即执行函数的方法 使用 ! 、+ 、- 等运算符
// -------方法一------
!function fun01(){
document.write("我在山里,你在湖里,爱却在云里" + "<br>");
}();
// -------方法二------
(function fun02(){
document.write("我在山里,你在湖里,爱却在云里" + "<br>");
})();
// -------方法三------
(function fun(){
document.write("我在山里,你在湖里,爱却在云里" + "<br>");
}())
</script>
- 函数的作用域
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
fun01();
function fun01(){
document.write("秋水共长天一色");
}
fun02();
let fun02 = function(){
document.write('<h1>我爱你中国</h1>');
};
</script>
函数fun02在调用时会报错 Cannot access 'fun02' before initialization 不能正常使用
let var 区别
<!DOCTYPE html>
<html lang="zn-CH">
<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</title>
</head>
<body>
</body>
</html>
<script>
for (let index = 0; index < 5; index++) {
var a = index;
let b = index;
c = index;
}
console.log("a的值为" + a);
console.log("c的值为" + c);
console.log("b的值为" + b); // Uncaught ReferenceError: b is not defined
</script>
var 是成员变量 let 是局部变量 当声明变量 没有使用 let 或 var 修饰的时候 默认隐含是 var修饰该变量 因此a c 的值能正常显示 b会报错