[Vuejs]-Operator replacement "with" in strict mode

0👍

Basically with is not recommended to be used because it can cause issues in programs. So what are your options?

define a bunch of globals

const sqrt = Math.sqrt
const log = Math.log

const test1 = new Function("a","return sqrt(a)")
const test2 = new Function("b","return log(b)")

console.log(test1(4))
console.log(test2(100))

Other option is to do the replacements with a reg exp

const fixMath = eq => eq.replace(/(sqrt|log|pow)/g,'Math.$1')

const test1 = new Function("a", fixMath("return sqrt(a)"))
const test2 = new Function("b", fixMath("return log(b)"))
const test3 = new Function("a", "b", fixMath("return pow(a, b)"))
const test4 = new Function("a", "b", fixMath("return sqrt(pow(a, 2) + pow(b, 2))"))

console.log(test1(4))
console.log(test2(100))
console.log(test3(10, 3))
console.log(test4(3, 3))

Leave a comment