Larry Ullman's Book Forums

# Roy

Members

6

0 Neutral

• Rank
Newbie
1. ## Question Regarding Named And Anonymous Funcitons Assigned To Variables

Thank you both for the information. It is greatly appreciated!
2. ## Question Regarding Named And Anonymous Funcitons Assigned To Variables

Let's see if I even got this right... From what I understand, the following creates a named function and assigns it to a variable: var someVar = function functionName() { //function body }; And this creates an anonymous function assigned to a variable: var someFunction = function() { //function body }; What I don't understand is what the functional difference is between the two, so could someone please explain this to me?
3. ## Chapter 6: Explicitely Defining Sparsely Populated Arrays And Conditions

Thank you for clarifying. I guess I was somewhat confused by the wording in the book
4. ## Chapter 6: Explicitely Defining Sparsely Populated Arrays And Conditions

In chapter 6, on page 230, it is said that when you, intentionally, want to create a sparsely populated arrays it is best to be explicit about it in the following way: var myList = [1, undefined, 3, undefined, 5]; However, this leads to a problem when using an if in condition in a for loop, like so: for (var i = 0, count = myList.length; i < count; i++) { if (i in myList) { console.log(myList[i]); } } Under normal circumstances, in a sparsely populated array, this would lead to only the defined elements (1, 3 and 5) outputting to the console. But when explicitely using undefined to create these "holes", it leads to the entire list, including undefined elements being outputted. Full example: var myList = [1, undefined, 3, undefined, 5]; for (var i = 0, count = myList.length; i < count; i++) { if (i in myList) { console.log(myList[i]); } } 1 undefined 3 undefined 5 This, however, does work: var myList = [1, , 3, , 5]; for (var i = 0, count = myList.length; i < count; i++) { if (i in myList) { console.log(myList[i]); } } 1 3 5

Thank you!