zoukankan      html  css  js  c++  java
  • => js 中箭头函数使用总结

    箭头函数感性认识
    
    箭头函数 是在es6 中添加的一种规范
    
    x => x * x 相当于 function(x){return x*x}
    
    箭头函数相当于 匿名函数, 简化了函数的定义。 语言的发展都是倾向于简洁 对人类友好的, 减轻工作量的。 就相当于我最钟情的python, 有很多类似之处, 在 关于promise 文章中我会和Python 框架中的tornado 中的异步进行对比的, 很相似。
    
    箭头函数有两种格式, 一种只包含一个表达式,没有{…} 和 return 。 一种包含多条语句, 这个时候{} return 就不能省略
    
    x => {
         if (x>0){
             return x*x
         }else{
            return x
         }
    }
    
    
    如果有多个参数就要用():
    
    // 两个参数返回后面的值
    (x, y) =>x*y + y*y
    //没有参数
    () => 999
    // 可变参数
    (x, y, ...rest) =>{
        var i,sum = x+y;
        for (i=0;i<rest.length;i++){
            sum += rest[i];
        }
        return sum;
    }
    
    如果要返回一个单对象, 就要注意, 如果是单表达式, 上面一种会报错, 要下面这种
    
    // 这样写会出错
    x => {foo:x} // 这和函数体{}有冲突
    // 写成这种
    x => {{foo:x}}
    
    
    箭头函数中this 指向
    
    箭头函数 看起来是匿名函数的简写。但是还是有不一样的地方。 箭头函数中的this是词法作用域, 由上写文确定
    
    var obj = {
        birth: 1990,
        getAge: function () {
            var b = this.birth; // 1990
            var fn = function () {
                return new Date().getFullYear() - this.birth; // this指向window或undefined
            };
            return fn();
        }
    };
    
    
    箭头函数修复了this的指向, this 总是指向词法作用域, 也就是外层调用者obj:
    
    var obj = {
        birth: 1990,
        getAge: function () {
            var b = this.birth; // 1990
            var fn = () => new Date().getFullYear() - this.birth; // this指向obj对象
            return fn();
        }
    };
    obj.getAge(); // 25
    
    
    如果使用箭头函数,以前的那种hack写法 就不需要了:
    var that = this;
    
    由于this 在箭头函数中已经按照词法作用域绑定了, 所以施一公call 或者apply() 调用函数的时候, 无法对this 进行绑定, 即 传入的第一个参数被忽略:
    
    var obj={
        birth:2018,
        getAge:function(year){
        var b =this.birth;//2018
        var fn = (y)=>y-this.birth //this.birth 仍然是2018
        return fn.call({birth:200},year)
    }
    }
    obj.getAge(2020)
    
    
    下面对比写es5 es6 直接关于箭头函数的对比使用
    
    //es5
    var fn = function(a, b){return a+b}
    //es6   直接被return时候可以省略函数体的括号
    const fn=(a,b) => a+b;
    
    //es5
    var foo = function(){
        var a=20;
        var b= 30;
        return a+b;
    }
    //es6
    const foo=()=>{
        const a= 20;
        const b=30;
        return a+b  
    }
    
    // 注意这里   箭头函数可以替代函数表达式但是不能替代函数声明
    
    
    
    再回到this 上面来,箭头函数样意义上面来说没有this. 如果使用了this 那么就一定是外层this .不会自动指向window对象。所以也就不能使用call/apply/bind来改变this指向
    
    var person = {
        name: 'tom',
        getName: function() {
            return this.name;
        }
    }
    
    使用es6 来重构上面的对象
    const person = {
        name:'tom',
        getName:()=>this.name
    }
    这样编译的结果就是
    var person ={
        name:'tom',
        getName:function getName(){return undefined.name}
    }
    
    
    
    在ES6中,会默认采用严格模式,因此this也不会自动指向window对象了,而箭头函数本身并没有this,因此this就只能是undefined,这一点,在使用的时候,一定要慎重慎重再慎重,不然踩了坑你都不知道自己错在哪!这种情况,如果你还想用this,就不要用使用箭头函数的写法。
    
    // 可以稍做改动
    const person = {
        name: 'tom',
        getName: function() {
            return setTimeout(() => this.name, 1000);
        }
    }
    
    // 编译之后变成
    var person = {
        name: 'tom',
        getName: function getName() {
            var _this = this;  // 使用了我们在es5时常用的方式保存this引用
    
            return setTimeout(function () {
                return _this.name;
            }, 1000);
        }
    };
  • 相关阅读:
    Docker03-镜像
    Docker02:Centos7.6安装Docker
    Docker01-重要概念
    WEB开发新人指南
    Lpad()和Rpad()函数
    Unable to find the requested .Net Framework Data Provider. It may not be installed
    redis自动过期
    redis简单的读写
    redis的安装
    Ajax缓存,减少后台服务器压力
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/10697136.html
Copyright © 2011-2022 走看看