Problem with arrays in javascript (v8)

Sep 5, 2014 at 8:24 AM
Not sure if I should post this as an issue, or it belongs here.

I'm using ClearScript to render pages (using handlebars), and seem to have a problem when iterating array in javascript (again, using handlebards).

Are the arrays not really arrays? If I iterate (using handlebars' #each), it looks like I get the elements in the array (as I should) and methods/properties of the array, or at least that's my guess.

Any input?
Sep 5, 2014 at 12:38 PM
Hello, and thanks for your question!

It looks like you're asking about host (.NET) arrays rather than JavaScript arrays. Is that correct?

ClearScript's general approach is to avoid data conversion and provide full access to .NET objects. There are exceptions, mainly numbers and strings, but host arrays are exposed "as is"; that is, they are not converted to script arrays.

Normally ClearScript exposes the members of a host object (fields, properties, and methods) as properties of the corresponding JavaScript "proxy" object. Additionally, if a host object implements IList, as arrays do, ClearScript also exposes its valid indices, enabling JavaScript's familiar foo[123] syntax for element access.

So yes, if you use JavaScript's statement to iterate over a host array, you'll get both its elements and its members, and it does appear that Handlebars's each helper works this way.

Consider applying the each helper not directly to a host array but to the output of a conversion function:
function convertArray(hostArray) {
    var result = [];
    for (var i = 0; i < hostArray.Length; i++)
    return result;
Another possibility might be to override the built-in helper with one that supports host arrays.