开发者

Array with a single element Javascript

I have a site that dynamically creates two arrays of Lat/Long values based on the stores that the currently logged in user can see. If the user can only see one location then I get an error about array length needing to be a finite integer. When I look开发者_如何学Go at the source I see

var ls =  new Array(45.056124);

is being created on the page dynamically which is what I'm expecting. Except I think it is treating it as if I am trying to set the length of the array instead of set the first element to that value.

How do I go about creating an array using the ClientScript.RegisterArrayDeclaration function to hold a single double value using vb.net?


Try doing this:

var ls =  [45.056124]; //sets it as an array of one value


This is one of JavaScript's bad parts. The Array constructor, when passed one Number argument (remember, in JavaScript all number literals are... Numbers), initializes the array to a predefined length.

That means:

var ls = new Array(45);

ls.length === 45; // This is true

When passed a non-integer Number, the Array constructor throws a RangeError indicating that it isn't a valid length.

As a rule of thumb, as it already been said, always use the array literal to create arrays:

var ls = [45.056124];


Doesn’t the documentation of the RegisterArrayDeclaration method give all the necessary information?

Your particular array would look as follows:

Page.ClientScript.RegisterArrayDeclaration("ls", "45.056124")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜