1

It is so funny that the following for in loop prints out 0 and 1. Anybody helps to explain: 1 what does it mean for variable in a lambda function? 2. why prints out 0 and 1? (I changed the ['a', 'b'] to [1,2], it still prints 0 and 1.

for (var f in d = function(){}, ['a', 'b']){console.log(f)}

prints out

0  
1 
  • 1
    [`for..in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loops always iterate an object's enumerable keys/properties. Even for `Array`s, `f` is being assigned the indices, [possibly among other keys](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea). – Jonathan Lonowski Jun 10 '15 at 22:16
  • It's not a syntax error, but in the above `d` has no purpose – vol7ron Jun 10 '15 at 22:22
  • 1
    @JonathanLonowski I don't think that's accurate. I believe the for..in is iterating over the last argument, in this case the array. `d` will still be a function at the end of the loop and has no purpose in the above. You could assign or use any number of variables `var b=[]; for( var p in a='',b,c=function(){},… ){ … }` – vol7ron Jun 10 '15 at 22:25
  • OK. For the line as `for (var f in d = function(){}){console.log(f)}`, function object seems to have no enumerable keys/properties, and nothing is printed out. Right? However, why doesn't engine report any error or warning such as this useless code? – user3470598 Jun 10 '15 at 22:26
  • @vol7ron Not a homework. I am working on a custom JavaScript translator. – user3470598 Jun 10 '15 at 22:28
  • @user3470598 A `Function` is still an `Object` in JavaScript and at least *could* have enumerable properties to iterate over. `(function(){} instanceof Object) // true`. – Jonathan Lonowski Jun 10 '15 at 22:28
  • "why doesn't engine report any error or warning such as this useless code" --- because it's not an error, it's a valid JS code. – zerkms Jun 10 '15 at 22:28
  • 1
    @JonathanLonowski "The function(){} is discarded by the comma operator, returning ['a', 'b'] to be assigned (=) to d" --- this is wrong. – zerkms Jun 10 '15 at 22:29
  • @user3470598 There is no such thing as a useless code error in JavaScript. JavaScript is interpreted. – vol7ron Jun 10 '15 at 22:29
  • @PHPglue Unfortunately, I can not change customers code. and the translator has to deal with it. – user3470598 Jun 10 '15 at 22:31
  • @zerkms exactly what i meant in my comment after his – vol7ron Jun 10 '15 at 22:38
  • Awesome! Didn't know it is such a great community! – user3470598 Jun 10 '15 at 22:39

3 Answers3

3

The

for (var f in d = function(){}, ['a', 'b']) ...

can be explained if you wrap the in clause into the parentheses:

for (var f in (d = function(){}, ['a', 'b']) ) 

where (d = function(){}, ['a', 'b']) is an expression enclosed in parentheses. The expression consists of 2 another expressions and a , operator.

, operator in turn evaluates both operands and returns the latter.

So in your case the d = function(){} assigns the anonymous function to a variable d and then the array with 2 elements is returned.

To summarize: the d = function(){} expression is not ignored and you can use d in the statements after this loop.

zerkms
  • 249,484
  • 69
  • 436
  • 539
1

This is a weird behavior, but to elaborate this, the javascript for...in loop through values in an array, but this array is the last parameter.

for (var f in d =null, ['a', 'b']){console.log(f)} //prints 0,1

for (var f in null, ['a', 'b'],['a', 'b','c'],['a', 'b','c','d']){console.log(f)}//prints 0,1,2,3

so basically it does not matter what d is, or other parameters, the last parameter, in this example ['a', 'b','c','d'], is the array that this for loop loop through

also note

for (var f in d =function(){}, ['a', 'b']){console.log(d[f])} //prints undefined

print undefined. the ['a','b'] is not assigned to d

Evilsanta
  • 1,032
  • 11
  • 18
0

You are logging the key of the array, so since it has 2 entries, it prints out 0 and 1. Maybe this will help you understand

for(var key in array){
  console.log(key, array[key]);
}

so the key (or index) would be a number representing where in the array your data is. You can then get your value by going array[key]

Shan Robertson
  • 2,742
  • 3
  • 25
  • 43
  • Tested. You are right. HOWEVER, what is the anonymous function `function(){}` doing here? Is it something to ignore completely? – user3470598 Jun 10 '15 at 22:20