An arrow function can usually be converted by replacing
(<args>) => <body>
with
function(<args>) { return <body>; }
So yours would be
rowCheckStatuses.reduce(function(a, b) { return a + b; }, 0)
There are exceptions to this rule so it's important that you read up on arrow functions if you want to know all of the differences. You should also note that arrow functions have a lexical this.
An arrow function can usually be converted by replacing
(<args>) => <body>
with
function(<args>) { return <body>; }
So yours would be
rowCheckStatuses.reduce(function(a, b) { return a + b; }, 0)
There are exceptions to this rule so it's important that you read up on arrow functions if you want to know all of the differences. You should also note that arrow functions have a lexical this.
You can refactor it as:
if( rowCheckStatuses.reduce(function(a, b){return a + b}, 0)
The initial accumulator isn't necessary (unless you expect the array to be empty sometimes), it could be:
if( rowCheckStatuses.reduce(function(a, b){return a + b})
This number acts as a boolean to determine whether or not there is at least one "1" in the array
It might be faster (and clearer) to use:
if( rowCheckStatuses.some(function(a){return a == 1}))
which will return true if there are any 1s in rowCheckStatuses and will return as soon as one is encountered. Another alternative is indexOf:
if( rowCheckStatuses.indexOf(1) != -1)
Lots of alternatives.
Arrow function: how to rewrite to regular function?
Converting Arrow function into regular function
javascript - change arrow function to normal function?
Es6 Arrow function to normal js - javascript
Videos
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. 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
It would look like this if you just converted the arrow function to a function function:
sortedArticles(): Article[] {
return this.articles.sort(function(a: Article, b: Article) { return b.votes - a.votes;});
// ---------------------------^^^^^^^^------------------------^^^-------------------------^^
}
...but note that there's more going on there than ES2015 ("ES6"). The : Article[] part is saying that sortedArticles returns an array of Article. (And similarly the : Article qualifiers on a and b.) That's not JavaScript at all. It looks like TypeScript.
The pure JavaScript version would just drop those type annotations:
sortedArticles() {
return this.articles.sort(function(a, b) { return b.votes - a.votes;});
}
But TypeScript's "fat arrow" functions work largely the same way ES2015's arrow functions do, so let's continue on the basis that we're talking about ES2015 arrow functions:
There are four fundamental differences1 between arrow functions and function functions:
They close over
this,super, and several other things,2 they don't have their own versions of those likefunctionfunctions do. (A consequence of this is that they can usesuperif they're defined in a context that can usesuper, whichfunctionfunctions cannot.)They can have a concise body rather than a verbose one (but they can have a verbose body as well).
They cannot be used as constructors. E.g., you can't use
newwith an arrow function. A consequence of this is that arrow functions don't have aprototypeproperty on them (since it's only used if the function is used withnew).There is no generator syntax for arrow functions. E.g., there is no arrow equivalent to
function *foo() { ... }.
These three functions are all effectively the same, since they don't use this or arguments:
// A `function` function
var f1 = function(x) {
return x * 2;
};
// An arrow function with a verbose body
var f2 = x => {
return x * 2;
};
// An arrow function with a concise body
var f3 = x => x * 2;
(If they used this or arguments, they would not be the same.)
Note that the concise body doesn't have a { after the => and must contain a single top-level expression (which can of course have subexpressions), which is used as the return value.
1 You'll find people telling you there's a fifth: That arrow functions cannot have a name. That's a myth. Arrow functions can have names; the arrow functions above have true names, f2 and f3 respectively. It's not just the variables that have names, the functions do as well.
2 Specifically, they close over this, super, arguments (the automatic pseudo-array of runtime arguments), and new.target.
A huge thank you to CodingIntrigue for pointing out several errors and omissions in the earlier versions of this answer.
If you're not familiar with arrow function, or if it's complicated, you can use JS Refactor, it's a Visual Studio Code extension. It can convert arrow function to normal function. Hope it helps someone.
Any expression after => in arrow functions becomes implicit return of function but You need to return explicitly in normal function using return keyword.
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = function(arr){
"use strict";
const squaredIntegers = arr.filter(function(num){
return Number.isInteger(num) && num > 0
});
return squaredIntegers;
};
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
I plugged your code into Babel and got this:
"use strict";
var realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
var squareList = function squareList(arr) {
"use strict";
var squaredIntegers = arr.filter(function (num) {
return Number.isInteger(num) && num > 0;
});
return squaredIntegers;
};
var squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
(in general, when you need to convert ES6+ syntax to ES5, you can use Babel to do it automatically.)