ECMAScript_6新特性学习-----箭头函数

箭头函数

  • 一. 箭头函数

一. 箭头函数

ES6 允许使用「箭头」(=>)定义函数。箭头函数中的this是向外层作用域中, 一层层查找this, 直到有this的定义.

通用写法

let fn = (arg1, arg2, arg3) => {
     
 return arg1 + arg2 + arg3;
}

箭头函数的注意点:

  1. 如果形参只有一个,则小括号可以省略
  2. 函数体如果只有一条语句,则花括号可以省略,函数的返回值为该条语句的
    执行结果
  3. 箭头函数 this 指向声明时所在作用域下 this 的值
  4. 箭头函数不能作为构造函数实例化
  5. 不能使用 arguments

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>箭头函数title>
head>

<body>
    <script>
        // ES6 允许使用「箭头」(=>)定义函数。
        //声明一个函数
        let fn1 = function(){
      

        }

        let fn2= (a,b)=>{
      
            return  a-b;
        }

        let fn3 = (a,b) => {
      
            return a + b;
        }
        //调用函数
        let result = fn2(1, 2);
        console.log(result);


        //1. this 是静态的. this 始终指向函数声明时所在作用域下的 this 的值
        function getName(){
      
            console.log(this.name);
        }
        let getName2 = () => {
      
            console.log(this.name);
        }

        //设置 window 对象的 name 属性
        window.name = '戏子';
        const school = {
      
            name: "戏子666"
        }

        //直接调用
        getName()   //戏子
        getName2(); //戏子

        //call 方法调用
        getName.call(school); //戏子666
        getName2.call(school); //戏子

        //2. 不能作为构造实例化对象
        // let Person = (name, age) => {
      
        //     this.name = name;
        //     this.age = age;
        // }
        // let me = new Person('xiao',30);
        // console.log(me);

        //3. 不能使用 arguments 变量
        // let fn = () => {
      
        //     console.log(arguments);
        // }
        // fn(1,2,3);

        //4. 箭头函数的简写
            //1) 省略小括号, 当形参有且只有一个的时候
            let add = n => {
      
                return n + n;
            }
            console.log(add(9));
            //2) 省略花括号, 当代码体只有一条语句的时候, 此时 return 必须省略
            // 而且语句的执行结果就是函数的返回值
            let pow = n => n * n;

            console.log(pow(8));

    script>
body>
html>

箭头函数 this 指向声明时所在作用域下 this 的值

ECMAScript_6新特性学习-----箭头函数_第1张图片

箭头函数不能作为构造函数实例化

ECMAScript_6新特性学习-----箭头函数_第2张图片

不能使用 arguments

在这里插入图片描述

箭头函数的简写

ECMAScript_6新特性学习-----箭头函数_第3张图片

箭头函数实例


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>箭头函数实践title>
    <style>
        div {
      
            width: 200px;
            height: 200px;
            background: #58a;
        }
    style>
head>
<body>
    <div id="ad">div>
    <script>
        //需求-1  点击 div 2s 后颜色变成『粉色』
        //获取元素
        let ad = document.getElementById('ad');
        //绑定事件
        ad.addEventListener("click", function(){
      
            //保存 this 的值
            // let _this = this;
            //定时器
            setTimeout(() => {
      
                //修改背景颜色 this
                // console.log(this);
                // _this.style.background = 'pink';
                this.style.background = 'pink';
            }, 2000);
        });

        //需求-2  从数组中返回偶数的元素
        const arr = [1,6,9,10,100,25];
        const result1 = arr.filter(function(item){
      
            if(item % 2 === 0){
      
                return true;
            }else{
      
                return false;
            }
        });

        const result2 = arr.filter(item => {
      
            if(item % 2 === 0){
      
                return true;
            }else{
      
                return false;
            }
        });


        const result3 = arr.filter(item => item % 2 === 0);

        console.log(result1);
        console.log(result2);
        console.log(result3);

        // 箭头函数适合与 this 无关的回调. 定时器, 数组的方法回调
        // 箭头函数不适合与 this 有关的回调.  事件回调, 对象的方法

    script>
body>

html>

ECMAScript_6新特性学习-----箭头函数_第4张图片

你可能感兴趣的:(#,ECMAScript_6新特性,javascript)