From the course: JavaScript: Closures

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Use JS features in place of closures

Use JS features in place of closures - JavaScript Tutorial

From the course: JavaScript: Closures

Use JS features in place of closures

- [Instructor] In recent versions, JavaScript has added a few features that enable you to control scope without building closures. ECMAScript 5 implemented the forEach method for arrays. We can use this method instead of a for loop to iterate through an array. Because forEach takes a callback function as its first argument, each time through the loop creates a separate local scope. Essentially, the work of creating a closure is already built into this method. So in the updateUIWorking method, we can recreate our stopwatch simulator using forEach instead of for to rewrite our code without a closure. So I'm going to comment out that whole for loop. Now, we use forEach with an array, so we need to start by putting the numbers we want to count through in an array and then referencing that. So I'm going to create a variable called count, and that's an array of zero, one, two, three, four, five, six, seven, eight, and nine.…

Contents