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 - JavaScript Tutorial
From the course: JavaScript: Best Practices for Functions and Classes
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Use function expressions instead of declarations6m 20s
-
Don't use the new keyword to construct functions2m 39s
-
Leave parameter values untouched3m 2s
-
Use arrow syntax for anonymous functions1m 36s
-
Rely on implicit return only when obvious3m 26s
-
Challenge: Create functions53s
-
Solution: Create functions5m 29s
-
-
-
-
-
-