You may not know the JavaScript new Function of method

  • 2020-03-30 02:37:35
  • OfStack

Most of us have used JavaScript, and the popularity of various JavaScript frameworks in recent years has made it more magical and easier. Anonymous function? I don't know what it is but it looks like what I wrote."
You may not know that you can pass new Function() a string as the body of a Function to construct a JavaScript Function. It's not often used in programming, but it should be useful sometimes.

Here is the basic use of new Function:

//The last argument is the body of the function, of type string.  
//The preceding arguments are the parameters (name)& NBSP; of the function to be constructed;
var myFunction = new Function('users', 'salary', 'return users * salary');  

Very simple, right?

Related articles: