简体   繁体   English

将箭头 function 更改为正常 function?

[英]change arrow function to normal function?

I have a simple function to play the previous video am using the arrow function我有一个简单的 function 使用箭头 function 播放上一个视频

Here is my solution这是我的解决方案

  $('#interactive-layers').on('click', ".go_back", function () {
        videohistory.pop();
        var hist = videohistory[videohistory.length - 1];
        videohistory.pop();


        if (hist[0].movieid == 4) {

            let movieData = movies.find(({
                movieid
            }) => movieid == 23);
            hist.push(movieData);
            playVideo(hist[3], hist[1], hist[2]);
        } else {
            playVideo(hist[0], hist[1], hist[2]);
        }
    });

This works fine in chrome, safari, firefox but in Internet explorer, I am getting the following error.这在 chrome、safari、firefox 中工作正常,但在 Internet Explorer 中,我收到以下错误。

SCRIPT1003: Expected ':'

How to change this function so that it can be compatibility with all browsers?如何更改此 function 使其与所有浏览器兼容?

try This,尝试这个,

$('#interactive-layers').on('click', ".go_back", function () {
        videohistory.pop();
        var hist = videohistory[videohistory.length - 1];
        videohistory.pop();

        if (hist[0].movieid == 4) {

            // Here I change the Arrow function with the es5 normal one.
            /*
               let movieData = movies.find(({ movieid }) => movieid == 23);
             */
            let movieData = movies.find(function(movieid){
              return movieid == 23;
            });
            hist.push(movieData);
            playVideo(hist[3], hist[1], hist[2]);
        } else {
            playVideo(hist[0], hist[1], hist[2]);
        }
    });

You need to convert it to the normal function instead of an arrow function.您需要将其转换为普通的function而不是箭头 function。 But keep in mind that you have used argument destructuring in the array.find() so you need to handle it properly while converting, like this:但请记住,您在array.find()中使用了参数解构,因此您需要在转换时正确处理它,如下所示:

let movieData = movies.find(function(entry) { return entry.movieid == 23; });

Also, I wouldn't use let either to make sure it will run on all IE browsers.另外,我不会使用let来确保它可以在所有 IE 浏览器上运行。 MDN states that it has partial support on IE so... use var instead to support all IE browsers. MDN 声明它对 IE 有部分支持,所以...使用var来支持所有 IE 浏览器。

Another thing mentioned above is that array.find() itself is not supported by IE, so you need to import a polyfill上面提到的另一件事是 IE 不支持array.find()本身,所以需要导入一个polyfill

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

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