From the course: JavaScript: Best Practices for Functions and Classes

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Don't use the new keyword to construct functions

Don't use the new keyword to construct functions - JavaScript Tutorial

From the course: JavaScript: Best Practices for Functions and Classes

Start my 1-month free trial

Don't use the new keyword to construct functions

- [Instructor] Among the many approaches to creating functions in JavaScript, the language supports a constructor. Much like you could use new object to create an object or new array to create an array. You can use a new function statement to create a function. The star file for this video, shows code that uses a new function statement to create a function. I pass in my parameter name as a string for the first argument, followed by my function statement as a string for the last argument. As with many aspects of coding though just because you can do something a certain way doesn't mean you should. And indeed you generally don't want to use a constructor to create a function. There were a few reasons for this but most importantly, it creates a potential security hole and it can slightly impact your app's performance. ESLint supports the no-new-func rule to flag function constructors and I want to add this to…

Contents