JS---函数的声明、调用及作用域

  • 函数声明及调用
<!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会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值