繁体   English   中英

如何在没有箭头函数的情况下重写这个 JS 方法?

[英]How to rewrite this JS Method without the Arrow Functions?

我需要从 animate.style 中删除以下方法的箭头函数; 我需要在不支持箭头功能的旧版 chromium 浏览器 (40) 上运行此代码。 我正在尝试将其改造成传统的遗留 function,但由于我的大脑似乎只能使用箭头函数,因此不断出现语法错误。 非常感谢您对这一挑战的任何帮助!

const animateCSS = (element, animation, prefix = 'animate__') =>
  // We create a Promise and return it
  new Promise((resolve, reject) => {
    const animationName = `${prefix}${animation}`;
    const node = document.querySelector(element);

    node.classList.add(`${prefix}animated`, animationName);

    // When the animation ends, we clean the classes and resolve the Promise
    function handleAnimationEnd(event) {
      event.stopPropagation();
      node.classList.remove(`${prefix}animated`, animationName);
      resolve('Animation ended');
    }

    node.addEventListener('animationend', handleAnimationEnd, {once: true});
  });

这就是我们使用上述方法的方式,其中还包含一个箭头 function:

animateCSS('.my-element', 'bounce');

// or
animateCSS('.my-element', 'bounce').then((message) => {
  // Do something after the animation
});

删除 =>。 在 function 块之前添加 function 关键字。 不要留下任何未包装的块。

const animateCSS = function (element, animation, prefix = 'animate__') {
  // We create a Promise and return it
  new Promise(function (resolve, reject) {
    const animationName = `${prefix}${animation}`;
    const node = document.querySelector(element);

    node.classList.add(`${prefix}animated`, animationName);

    // When the animation ends, we clean the classes and resolve the Promise
    function handleAnimationEnd(event) {
      event.stopPropagation();
      node.classList.remove(`${prefix}animated`, animationName);
      resolve('Animation ended');
    }

    node.addEventListener('animationend', handleAnimationEnd, {once: true});
  });
  }
  
  animateCSS('.my-element', 'bounce');

// or
animateCSS('.my-element', 'bounce').then(function (message) {
  // Do something after the animation
});```

暂无
暂无

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

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