I want to create C type enumerations, values automatically starting from 0.
What is wrong with the following code?
function ArrayToEnum(arr)
{
var len = arr.lenght;
en = {};
for(i=0;i<len;i++)
en[arr[i]]=i;
return en;
}
a=['foo','bar'];
b=ArrayToEnum(arr);
console.debug(b.foo);
> Undefined
I expect it to print 0 but instead(at least in Chromium 9.0) this is undefined. If instead of calling the function, I apply the body directly, it works:
var l=a.length;
for(i=0;i<l;i++) b[a[i]]=i;
console.debug(b.foo);
> 0
What is wrong with the function?
lengthin the 3rd line of your code. Does that error exist in your actual code as well? Also - what's the point of this?