让我们说:
function MapMePls (str, func, ...args) {
return str.func(...args);
}
MapMePls('Hello World!', toLowerCase);
func
可以是String
的任何原型函数。
答案 0 :(得分:1)
您可以将函数的名称作为string
传递,并使用[]
语法来访问它。还要添加一个检查,以确保传递的名称是实际功能名称。
function MapMePls (str, func, ...args) {
if(!str[func] || typeof str[func] !== 'function') {
throw new Error('function does not exist');
}
return str[func](...args);
}
console.log(MapMePls('Hello World!', 'toLowerCase'));