前端入门09 -- JavaScript之函数,作用域链,闭包

2022-09-21  本文已影响0人  YanZi_33

函数

    <script>
        ///1.function关键字
        ///addNum是一个函数
        function addNum(a,b) {
            return a + b
        }
        addNum(100,200)
        ///2.函数表达式 
        ///func是变量名 不是函数名 后面的表达式才是函数 属于匿名函数
        var func = function(x) {
            console.log(x)
        }
        func('yanzi')

        ///3.箭头函数
        var func2 = () => {
            console.log('我是一个箭头函数');
        }
        func2();
    </script>
    <script>
        //定义声明函数
        function getSum() {
            var sum = 0
            for (var i = 0; i < 100; i++) {
                sum += i
            }   
            console.log(sum);
        }
        //调用函数
        getSum()
    </script>

函数的参数

<!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</title>
    <script>
        //定义声明函数
        function getSum(num1,num2) {
            console.log(num1 + num2)
        }
        //调用函数
        getSum(100,200)
    </script>
</head>
<body>
</body>
</html>
<!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</title>
    <script>
        //定义声明函数
        function getSum(num1,num2) {
            console.log(num1 + num2)
        }
        getSum(100,200) //300
        getSum(100,200,300) //300
        getSum(100) //NaN
    </script>
</head>
<body>
</body>
</html>
箭头函数的参数与参数默认值
  <script>

    const fn = (a,b) => {
      console.log('a =',a);
      console.log('b =',b);
    }

    //省略() 与 {}
    const fn1 = a => console.log('a =',a);

    //参数默认值
    const fn2 = (a,b,c = 30) => {
      console.log('a =',a);
      console.log('b =',b);
      console.log('c =',c);
    }

  </script>
Object对象作为函数参数
  <script>

    function fn(a) {
      console.log('a =', a);
    }

    let obj = { name: 'yanzi' };
    fn(obj);

  </script>
  <script>

    function func(a = { name: '大神' }) {
      console.log(a.name);
      a.name = '小神';
      console.log(a.name);
    }
    func(); // 大神 小神
    func(); // 大神 小神

    let obj = { name: '大神' }
    function func1(a = obj) {
      console.log(a.name);
      a.name = '小神';
      console.log(a.name);
    }
    func1(); // 大神 小神
    func1(); // 小神 小神

  </script>
函数作为函数参数
  <script>

    function func(a) {
      console.log('a =', a);
    }

    //自定义函数
    function func1() {
      console.log('自定义函数');
    }

    func(func1);

    //匿名函数表达式
    func(function () {
      console.log('匿名函数表达式');
    })

    //箭头函数
    func(() => {
      console.log('箭头函数');
    })

  </script>
arguments的使用
    <script>
        function fn() {
            console.log(arguments)
            console.log(arguments.length)
            console.log(arguments[2])
            for (var i = 0; i < arguments.length; i++) {
                console.log(arguments[i]); //10,2,33
            }
        }
        fn(10,2,33)
    </script>
函数返回值
    <script>
        //定义声明函数
        function getArrMax(arr) {
            var max = arr[0]
            for (var i = 1; i < arr.length; i++) {
                if (arr[i] > max) {
                    max = arr[i]
                }
            }
            return max
        }

        var max = getArrMax([100,200,30,40,50,333,234])
        console.log(max);
    </script>
    <script>
        function getResult(num1,num2) {
            //返回值为数组
            return [num1+num2,num1-num2]
        }
        var result = getResult(100,30)
        console.log(result);
    </script>
  <script>

    const func = (a, b) => {
      return a + b;
    }

    const func1 = (a, b) => a + b;

    const func2 = (a, b) => ({ name: 'yanzi' });

  </script>

JavaScript作用域

    <script>
        //全局作用域 全局变量
        var num = 100

        function addNum(a,b) {
            //局部作用域 局部变量
            var sum = 0
            sum = a + b
            //sum1没有声明直接赋值 属于全局变量
            sum1 = 1000
            return sum
        }
        addNum(100,200)
    </script>
  <script>

    var num = 10
    function func() { //外部函数
      var num = 20
      function func1() { //内部函数
        console.log(num); // 20
      }
    }

    let a = '全局作用域中的a';
    {
      let a = '第一个块级作用域中的a';
      {
        let a = '第二个块级作用域中的a';
        console.log('a =', a);
      }
    }

  </script>

JavaScript预解析

  <script>

    console.log(a); //a is not defined
    a = 100;

    console.log(b); //undefined
    var b = 100;

    console.log(c); //Cannot access 'c' before initialization
    let c = 100;

  </script>
    <script>
        //第一种:
        console.log(num) //报错

        //第二种:
        console.log(num) //undefine
        var num = 10

        //第三种
        func() //111
        function func() {
            console.log(111)
        }

        //第四种
        fun() //报错
        var fun = function() {
            console.log(222)
        }
    </script>
    <script>
        var a = 18
        f1()

        function f1() {
            var b = 9
            console.log(a)
            console.log(b)
            var a = '123'
        }

        //上述代码等价于
        var a
        function f1() {
            var b 
            var a
            b = 9
            console.log(a) //undefine 
            console.log(b) //9
            a = '123'
        }
        a = 18
        f1()
    </script>
    <script>
        f1()
        console.log(c)
        console.log(b)
        console.log(a)

        function f1() {
            // var a = 9; b = 9;c = 9 b与c没有声明 直接赋值 属于全局变量
            // var a = b = c = 9 与 var a = 9,b = 9, c = 9 是不同的
            var a = b = c = 9
            console.log(a)
            console.log(b)
            console.log(c)
        }
        
        //以上代码等价于
        function f1() {
            var a
            a = b = c = 9
            console.log(a) //9
            console.log(b) //9
            console.log(c) //9
        }

        f1()
        console.log(c) //9
        console.log(b) //9
        console.log(a) //报错 a是局部变量
    </script>

立即执行函数

  <script>

    (function(){
      console.log('立即执行函数');
    })()

  <script>

函数中的this关键字

  <script>

    function func() {
      console.log(this);
    }
    //以函数的形式调用时,this指向window
    func(); //window

    //以方法的形式调用时,this指向调用方法的对象
    const obj = { name: '小神' };
    obj.test = func;
    obj.test(); //obj

    const obj2 = { name: '大神', test: func };
    obj2.test(); //obj2

  </script>
<script>

    function func() {
      console.log(this);
    }
    //以函数的形式调用时,this指向window
    func(); //window

    //箭头函数
    const func3 = (a, b) => {
      var num = a + b;
      console.log(this);
      return num;
    }
    func3(); //外层作用域为全局作用域 this指向window

    const obj3 = {
      name: '如来',
      fn: func,
      fn2: func3,
      sayHello() {
        console.log(this.name);
        function t() {
          console.log('t -->', this);
        }
        t(); //window

        const t2 = () => {
          console.log('t2 -->', this);
        }
        t2(); //obj3
      }
    }
    obj3.fn();  //obj3
    obj3.fn2(); //window

    obj3.sayHello();

  </script>

高阶函数

  <script>

    //高阶函数 -- 参数为一个函数
    function filter(arr, cb) {
      const newArr = [];
      for (let index = 0; index < arr.length; index++) {
        const element = arr[index];
        if (cb(arr[index])) {
          newArr.push(arr[index]);
        }
      }
      return newArr;
    }

    const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var result = filter(arr, a => a % 2 === 0);
    console.log(result);

  </script>

闭包

  <script>

    function outer() {
      let num = 0;
      return () => {
        num++;
        console.log(num);
      }
    }

    const newFn = outer;
    newFn();

  </script>
  <script>

    let a = '全局变量a';
    function fn() {
      console.log(a);
    }

    function fn2() {
      let a = 'fn2中的a';
      fn();
    }
    fn2();  //fn() --> 打印的是全局变量a 

    function fn3() {
      let a = 'fn3中的a';
      function fn4() {
        console.log(a);
      }
      return fn4;
    }

    let result = fn3();
    fn4(); //fn4() --> 打印的是fn3中的a
    
  </script>
  <script>

    function outer() {
      let someValue = 'some';
      return function () {
        console.log(someValue);
      }
    }

  </script>
上一篇下一篇

猜你喜欢

热点阅读