简体   繁体   中英

How can I have prototype function as parameters?

Let say:

function MapMePls (str, func, ...args) {
  return str.func(...args);
}

MapMePls('Hello World!', toLowerCase);

The func can be any prototype functions from String .

You can pass the name of the function as string and use [] syntax to access it. Also add a checking to be sure that the passed name is an actual function name .

 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')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM