使用 Function() 构造函数作为闭包
Using Function() constructor as a closure
我正在尝试做这样的事情:
function simpleOperations(operation) {
let myFanction = new Function('a', 'b', 'a ' + operation + ' b');
return myFanction
}
let sum = simpleOperations("+")
let multiplicate = simpleOperations("*")
console.log("your sum is: " + sum(3,7));
console.log("your product is: " + multiplicate(3,7));
而不是得到:
your sum is: 10
your product is: 21
我明白了:
your sum is: undefined
your product is: undefined
您对如何修复它有任何想法吗? :)
函数的文本主体需要 return
值,否则你的 a + b
或 a * b
等将只是一个未使用的表达式。
function simpleOperations(operation) {
let myFanction = new Function('a', 'b', 'return a ' + operation + ' b');
return myFanction
}
let sum = simpleOperations("+")
let multiplicate = simpleOperations("*")
console.log("your sum is: " + sum(3,7));
console.log("your product is: " + multiplicate(3,7));
使用Function
是一种反模式。它与 eval
相差一个度,并且总是有更好的写法,特别是考虑到 JavaScript 已经支持 first-class 函数 -
const add = (a, b) => a + b
const multiply = (a, b) => a * b
console.log("your sum is: " + add(3,7))
console.log("your product is: " + multiply(3,7))
your sum is: 10
your product is: 21
如果您想使用 "+"
和 "*"
字符串访问函数,您可以创建一个简单的查找并在不支持特定操作时抛出错误 -
const operations = {
"+": (a, b) => a + b,
"*": (a, b) => a * b
}
function simpleOperations(op) {
const f = operations[op]
if (f == null)
throw Error(`unsupported operation: ${op}`)
return f
}
const add = simpleOperations("+")
const multiply = simpleOperations("*")
console.log("your sum is: " + add(3,7))
console.log("your product is: " + multiply(3,7))
const divide = simpleOperations("/") // <- not yet implemented
your sum is: 10
your product is: 21
Error: unsupported operation: /
实现一个简单的计算器是学习如何编写您的第一门编程语言的好方法。如果您对这类事情感兴趣,请参阅此 。
我正在尝试做这样的事情:
function simpleOperations(operation) {
let myFanction = new Function('a', 'b', 'a ' + operation + ' b');
return myFanction
}
let sum = simpleOperations("+")
let multiplicate = simpleOperations("*")
console.log("your sum is: " + sum(3,7));
console.log("your product is: " + multiplicate(3,7));
而不是得到:
your sum is: 10
your product is: 21
我明白了:
your sum is: undefined
your product is: undefined
您对如何修复它有任何想法吗? :)
函数的文本主体需要 return
值,否则你的 a + b
或 a * b
等将只是一个未使用的表达式。
function simpleOperations(operation) {
let myFanction = new Function('a', 'b', 'return a ' + operation + ' b');
return myFanction
}
let sum = simpleOperations("+")
let multiplicate = simpleOperations("*")
console.log("your sum is: " + sum(3,7));
console.log("your product is: " + multiplicate(3,7));
使用Function
是一种反模式。它与 eval
相差一个度,并且总是有更好的写法,特别是考虑到 JavaScript 已经支持 first-class 函数 -
const add = (a, b) => a + b
const multiply = (a, b) => a * b
console.log("your sum is: " + add(3,7))
console.log("your product is: " + multiply(3,7))
your sum is: 10
your product is: 21
如果您想使用 "+"
和 "*"
字符串访问函数,您可以创建一个简单的查找并在不支持特定操作时抛出错误 -
const operations = {
"+": (a, b) => a + b,
"*": (a, b) => a * b
}
function simpleOperations(op) {
const f = operations[op]
if (f == null)
throw Error(`unsupported operation: ${op}`)
return f
}
const add = simpleOperations("+")
const multiply = simpleOperations("*")
console.log("your sum is: " + add(3,7))
console.log("your product is: " + multiply(3,7))
const divide = simpleOperations("/") // <- not yet implemented
your sum is: 10
your product is: 21
Error: unsupported operation: /
实现一个简单的计算器是学习如何编写您的第一门编程语言的好方法。如果您对这类事情感兴趣,请参阅此