简体   繁体   English

无法将此ES6转换为ES5,包括扩展语法

[英]Having trouble translating this ES6 to ES5 including spread syntax

I am trying to translate the following into ES5, while learning the language: 我正在尝试将以下内容翻译成ES5,同时学习语言:

const transitionendFn = ()=> {
    [...this.slider].forEach((el)=> el.className = 'item');
    selectedEl.classList.add('active');
    this.isAnimating = false
}

ES5: ES5:

const transitionendFn = function() {
    [].concat(this.slider).
        forEach(function (el) {
            return el.className = 'item';
        });
    selectedEl.classList.add('active');
    this.isAnimating = false
}

I don't understand the spread part. 我不明白传播部分。

this.slider contains the following: this.slider包含以下内容:

在此输入图像描述

Any help correcting this code is appreciated. 任何纠正此代码的帮助表示赞赏。

I get “ TypeError : el is undefined” with my translation. 我的翻译得到“ TypeErrorel is undefined”。

Be aware that: 意识到:

  • Arrow functions do not have a this binding. 箭头功能没有this绑定。 Traditional functions (the only kind available in ES5) do, so you will have to bind it to the desired value. 传统函数(ES5中唯一可用的类型)可以,因此您必须将其绑定到所需的值。
  • ES5 does not have const variables. ES5没有const变量。 Only var ones. 只有var
  • ES5 does not have iterable objects. ES5没有可迭代的对象。 I will assume this.slider is array-like. 我假设this.slider是类似数组的。

Then the translation would be something like 那么翻译就像是

var transitionendFn = function() {
  [].forEach.call(this.slider, function(el) {
    return el.className = 'item';
  });
  selectedEl.classList.add('active');
  this.isAnimating = false;
}.bind(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM