简体   繁体   English

Javascript-在ES5中扩展ES6类

[英]Javascript - Extend an ES6 Class in ES5

I am using the following code for a slider with Siema : 我将以下代码用于带有Siema的滑块:

https://codepen.io/pawelgrzybek/pen/boQQWy https://codepen.io/pawelgrzybek/pen/boQQWy

Which uses extending classes to add dots to the slide. 它使用扩展类在幻灯片上添加点。 All works well except that our site is now having issues with Googles Mobile Friendly Test with it using ES6 as it gives the error: 一切正常,除了我们的网站使用ES6进行的Google移动友好测试存在问题之外,因为它会出现以下错误:

Uncaught SyntaxError: Unexpected reserved word

on this line: 在这条线上:

class SiemaWithDots extends Siema {

Is there a way I can make this compatible with ES5? 有什么办法可以使它与ES5兼容?

The code can be seen below: 该代码可以在下面看到:

// instantiate new extended Siema
const mySiemaWithDots = new SiemaWithDots({
  // on init trigger method created above
  onInit: function(){
    this.addDots();
    this.updateDots();
  },

  // on change trigger method created above
  onChange: function(){
    this.updateDots()
  },
});

// extend a Siema class by two methods
// addDots - to create a markup for dots
// updateDots - to update classes on dots on change callback
class SiemaWithDots extends Siema {

  addDots() {
    // create a contnier for all dots
    // add a class 'dots' for styling reason
    this.dots = document.createElement('div');
    this.dots.classList.add('dots');

    // loop through slides to create a number of dots
    for(let i = 0; i < this.innerElements.length; i++) {
      // create a dot
      const dot = document.createElement('button');

      // add a class to dot
      dot.classList.add('dots__item');

      // add an event handler to each of them
      dot.addEventListener('click', () => {
        this.goTo(i);
      })

      // append dot to a container for all of them
      this.dots.appendChild(dot);
    }

    // add the container full of dots after selector
    this.selector.parentNode.insertBefore(this.dots, this.selector.nextSibling);
  }

  updateDots() {
    // loop through all dots
    for(let i = 0; i < this.dots.querySelectorAll('button').length; i++) {
      // if current dot matches currentSlide prop, add a class to it, remove otherwise
      const addOrRemove = this.currentSlide === i ? 'add' : 'remove';
      this.dots.querySelectorAll('button')[i].classList[addOrRemove]('dots__item--active');
    }
  }
}

You would then replace the class with an old-style constructor function, and then manipulate the prototype to set up the prototype hierarchy: 然后,您可以用老式的构造函数替换class ,然后操纵原型以建立原型层次结构:

function SiemaWithDots() {
    Siema.apply(this, arguments);
}

SiemaWithDots.prototype = Object.create(Siema.prototype);
SiemaWithDots.prototype.constructor = SiemaWithDots;
SiemaWithDots.prototype.addDots = function () {
    // ... your code ...
};
SiemaWithDots.prototype.updateDots = function () {
    // ... your code ...
};

Currently, your code contains a lot of ES6 features - const , class , arrow functions. 目前,您的代码包含了很多功能ES6 - constclass ,箭头功能。 You should use an online transpiler like Babel.JS to make your code ES5-compatible. 您应该使用Babel.JS之类的在线编译来使代码与ES5兼容。

Also, in your code, you're using a class before you define it. 另外,在代码中,在定义类之前先使用一个类。 It's good practice to move all class definitions and arrow functions to the top of your code (normal ES5 functions are located before any code is evaluated). 好的做法是将所有类定义和箭头函数移到代码的顶部(在评估任何代码之前,先找到常规的ES5函数)。 So your code should look like: 因此,您的代码如下所示:

class SiemaWithDots extends Siema {...}

const mySiemaWithDots = new SiemaWithDots({...});

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

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