The Dead Man
The Dead Man

Reputation: 5566

change arrow function to normal function?

I have a simple function to play the previous video am using the arrow 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.

SCRIPT1003: Expected ':'

How to change this function so that it can be compatibility with all browsers?

Upvotes: 0

Views: 121

Answers (2)

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

You need to convert it to the normal function instead of an arrow 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:

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. MDN states that it has partial support on IE so... use var instead to support all IE browsers.

Another thing mentioned above is that array.find() itself is not supported by IE, so you need to import a polyfill

Upvotes: 1

Ahmed A. Mahmoud
Ahmed A. Mahmoud

Reputation: 79

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]);
        }
    });

Upvotes: 1

Related Questions