You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Larregoity (Created) (JIRA)" <ji...@apache.org> on 2012/03/28 12:14:28 UTC

[jira] [Created] (THRIFT-1545) Generated javascript code uses "for in" for looping over arrays

Generated javascript code uses "for in" for looping over arrays
---------------------------------------------------------------

                 Key: THRIFT-1545
                 URL: https://issues.apache.org/jira/browse/THRIFT-1545
             Project: Thrift
          Issue Type: Bug
          Components: JavaScript - Compiler
    Affects Versions: 0.8
            Reporter: Larregoity
            Priority: Minor


For lists, generated javascript is like :
>>

GetRepairersResponse.prototype.write = function(output){ 
output.writeStructBegin('GetRepairersResponse')
if (null != this.repairers) {
output.writeFieldBegin('repairers', Thrift.Type.LIST, 1)
{
output.writeListBegin(Thrift.Type.STRUCT, this.repairers.length)
{
for(var iter62 in this.repairers)
{
iter62=this.repairers[iter62]
iter62.write(output)
}
}
output.writeListEnd()
}
output.writeFieldEnd()
}
output.writeFieldStop()
output.writeStructEnd()
return
}
>>

The use of "for in" generates problems when properties or functions are added to the Array object (for instance in the sencha library, that adds the "indexOf", "remove" and "contains" methods), because these properties will be included in the for loop.

As said in https://developer.mozilla.org/en/JavaScript/Guide/Predefined_Core_Objects,: "Since JavaScript elements are saved as standard object properties, it is not advisable to iterate through JavaScript arrays using for...in loops because normal elements and all enumerable properties will be listed."

It would be much safer if the generated code used standard for loops to  like :
>>
var colors = ['red', 'green', 'blue'];  
for (var i = 0; i < colors.length; i++) {  
  console.log(colors[i]);  
}  
>> 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira