So I was looking up the JavaScript function eval() and the Mozilla article on it said you should never use it, because it's vulnerable to attacks. It said you should use Function() instead. There were examples of converting code from one to the either, but also examples where they simply removed eval(). None of them really matched to my code, so I just wanted to get mine checked and also clarify something. So here's the original and the converted code:
if (arrCount.length === 1) {
count = eval(arrIng[0].replace('-', '+'));
} else {
count = eval(arrIng.slice(0, unitIndex).join('+'));
};
if (arrCount.length === 1) {
count = Function('"use strict";return arrIng[0].replace('-',
'+')');
} else {
count = Function('"use strict";return arrIng.slice(0,
unitIndex).join('+')');
};
Are there any mistakes? Or should I simply remove the eval()? Also, I saw that Function() creates functions that only execute in the global scope. Will that cause a problem with this code? It is a couple blocks inside a map method. If you need the rest of the method for context, let me know. Thanks!
Aucun commentaire:
Enregistrer un commentaire