Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ES6 Arrow Function to ES5

I found a function I can use in a Leaflet project I’m working. The function is written is ES6 and it works great in both Firefox and Chrome. However, I need to target IE as well. In my research I found IE at present don't accept the ES6 Arrow function. I also I found if the ES6 function was converted to ES5, the function will work in IE. For days now I tried to convert the following function to ES5 but to no prevail. Some of the solutions I tried were found posted here. Can some please look at my script and let me know what I’m doing wrong. Also, what is the benefit of ES6 anyway; shorter script? Thank you in advance.

Here’s the working ES6 script:

points.map((p, i) => L.marker(p).bindPopup(`marker${'<strong>' + pointData[i].cityName + '</strong>' + ', ' + '</br>'  + pointData[i].discrip +  "<br /><a class='fancybox-thumb' ' style='width:150px;' rel='fancybox-button'  rel='fancybox-thumb'   data-fancybox-group='"+ pointData[i].popup +"'   title='" + pointData[i].discrip + " '  href='graphic/"+ pointData[i].popup + "' ><img src='graphic/" + pointData[i].popup + "' alt='Image' ' style='width:150px;' ></a>" + "<br/><a href='http://www.google.com'  target='_blank'>Cedellion Report</a>"}`))
.forEach(function(marker) {
    map.addLayer(marker);
    oms.addMarker(marker);
});

Here’s my best attempt/guess with no joy.

points.map(function(p, i) {
L.marker(p).bindPopup(`marker${'<strong>' + pointData[i].cityName + '</strong>' + ', ' + '</br>'  + pointData[i].discrip +  "<br /><a class='fancybox-thumb' ' style='width:150px;' rel='fancybox-button'  rel='fancybox-thumb'   data-fancybox-group='"+ pointData[i].popup +"'   title='" + pointData[i].discrip + " '  href='graphic/"+ pointData[i].popup + "' ><img src='graphic/" + pointData[i].popup + "' alt='Image' ' style='width:150px;' ></a>" + "<br/><a href='http://www.google.com'  target='_blank'>Cedellion Report</a>"}`)})
.forEach(function(marker) {
map.addLayer(marker);
oms.addMarker(marker);
});
like image 492
TonyT Avatar asked Mar 22 '19 23:03

TonyT


People also ask

Is this arrow function of ES6 same as function of ES5?

There is a noticeable difference between the syntax of es5 functions and es6 arrow functions. //es6 arrow functions const printHelloWorld = () => console. log('Hello World'); In the above syntax we can put the curly braces {} after the token=>.

Is arrow function available in ES5?

The arrow function concept was introduced in ES6. With the help of this, we can write a shorter and concise syntax for a normal function which we used to write in ES5. Note: In arrow function, if there is only one statement then we don't even need to use '{}' curly braces.

Is ES6 compatible with ES5?

As of now, there are no browsers that fully support the ES6 features; however, we can convert the ES6 code to the ES5 code by using the transpilation. There are two major compilers Babel and Traceur, which are used to convert the ES6 code to ES5 code as part of the build process.


1 Answers

When you have ES6+ code that you want to make compatible for ES5, to transpile the syntax, you can do it automatically with a transpiler like Babel. Plugging in your code gives this result:

points.map(function (p, i) {
  return L.marker(p).bindPopup("marker" + ('<strong>' + pointData[i].cityName + '</strong>' + ', ' + '</br>' + pointData[i].discrip + "<br /><a class='fancybox-thumb' ' style='width:150px;' rel='fancybox-button'  rel='fancybox-thumb'   data-fancybox-group='" + pointData[i].popup + "'   title='" + pointData[i].discrip + " '  href='graphic/" + pointData[i].popup + "' ><img src='graphic/" + pointData[i].popup + "' alt='Image' ' style='width:150px;' ></a>" + "<br/><a href='http://www.google.com'  target='_blank'>Cedellion Report</a>"));
}).forEach(function (marker) {
  map.addLayer(marker);
  oms.addMarker(marker);
});

You needed to transpile the template literal too - declare strings and concatenate with + instead of using ${} syntax. In addition, you needed to return the L.marker... from the .map callback.

Note that this only transpiles syntax, not methods - if you're using ES6+ methods (for example, Array.prototype.includes), Babel won't be enough - you'll either need to change the code manually to use an ES5 method (like indexOf), or, a better option, include a polyfill (example) to define the ES6+ methods on clients viewing your page.

like image 110
CertainPerformance Avatar answered Sep 20 '22 19:09

CertainPerformance