锋芒神话霜 2020-03-29 09:56 采纳率: 50%
浏览 167
已结题

关于ES6解构赋值我有不理解的地方

//绑定元素
    Img.prototype._bind = function() {
      console.log('classfied',this.classified)
      console.log('all',this.all)
      console.log('this.figures',this.figures)
      methods.$('.__Img__classify' , this.wrap).addEventListener('click',(e) =>{
        if(e.target.nodeName !== 'LI') return;
        console.log("Img.prototype._bind -> target", e.target)

        const type = e.target.innerText
        const els = this._getImgsByType(type)      

        let prevImgs = this.figures.map(figure =>methods.$('img',figure).src) //找出当前每个fugure中包含的图片的src
        console.log("Img.prototype._bind -> prevImgs", prevImgs)

        let nextImgs = els.map(figure => methods.$('img',figure).src)

        const diffArr = this._diff(prevImgs,nextImgs)

        console.log('diffArr',diffArr)
      })
    }

这是老师教的ES6切换图片中绑定元素的实现方法,我不太理解的是第七行那个(e),现在是我自己改好的,也有相同的效果
老师原本的是这样的

//绑定元素
    Img.prototype._bind = function() {
      console.log('classfied',this.classified)
      console.log('all',this.all)
      console.log('this.figures',this.figures)
      methods.$('.__Img__classify' , this.wrap).addEventListener('click',({target}) =>{
        if(target.nodeName !== 'LI') return;
        console.log("Img.prototype._bind -> target", target)

        const type = target.innerText
        const els = this._getImgsByType(type)      

        let prevImgs = this.figures.map(figure =>methods.$('img',figure).src) //找出当前每个fugure中包含的图片的src
        console.log("Img.prototype._bind -> prevImgs", prevImgs)

        let nextImgs = els.map(figure => methods.$('img',figure).src)

        const diffArr = this._diff(prevImgs,nextImgs)

        console.log('diffArr',diffArr)
      })
    }

那里面的{target}是因为解构赋值才默认是e.target吗,箭头函数这样写解购赋值一定默认最初的参数是e吗,什么时候解构赋值用[]什么时候用{}啊,求教,有点晕

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-03-29 10:21
    关注

    e是lambda表达式(箭头函数)的参数,这是一个回调函数,按了按钮以后,会自动传进来。
    因为是参数,写e或者abc,eee都是可以的(只要参数和里面的使用一致)
    好比
    function isOdd(a) { return a % 2 == 0 };

    function isOdd(aaa) { return aaa % 2 == 0 };
    是一样的。

    []是列表,{}是对象。

    评论

报告相同问题?