Reputation: 27441
So, this may be a really stupid question, but I'm obviously missing something here.
Consider the following code:
var selectedItems = [];
selectedItems.push("0ce49e98-a8aa-46ad-bc25-3a49d475e9d3");
//fyi, selectedItems[selectedItems.length] = "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3"; produced the same result.
At the end selectedItems
content looks like this:
Name Value Type
------------- -------------------------------------- ------
selectedItems {...} Object
- [0] "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3" String
- length 1 Long
But if I just try to call split() on the same string, like this:
selectedItems = "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3".split(",")
Now the content of my supposed array looks like this (missing length):
Name Value Type
------------- -------------------------------------- ------
selectedItems {...} Object
- [0] "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3" String
Any idea what the difference is? What's actually happening here?
Thanks in advance.
UPDATED: I have a feeling there's actually something structurally different about the two resulting values, because (atlas) ajax chokes on the one with the length property when I try to pass it to a server-side WebMethod (no actual error message, but I know the call fails). I'm not sure.
UPDATE #2 I noticed that setting the targetLocationIdList this way, results in no 'length' property being displayed in Quick Watch window:
var params =
{
jobId : args.get_JobId(),
targetLocationIdList : retVal.split(',')
};
But this results contain 'length' property displayed in Quick Watch window:
var retValArr = [];
retValArr = retVal.split(',');
var params =
{
jobId : args.get_JobId(),
targetLocationIdList : retValArr
};
Upvotes: 3
Views: 557
Reputation: 14529
Here's a very simple test:
var selectedItems = [];
selectedItems.push("0ce49e98-a8aa-46ad-bc25-3a49d475e9d3");
alert(selectedItems);
selectedItems = "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3".split(",");
alert("boo");
alert(selectedItems.length);
After boo, we check if there is still a length.
Answer: yes. So in other words, this is an error of Visual Studio, that and nothing more.
Upvotes: 0
Reputation: 6428
It looks like you've found a defect in Microsoft's Javascript implementation. It's likely that Visual Studio and IE share the same implementation so that's why you're experiencing the error in just those two programs and not in Firefox or Chrome.
In the EMCAScript standard: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
15.5.4.14 String.prototype.split (separator, limit)
Returns an Array object into which substrings of the result of converting this object to a string have been stored.
In the definition of Array Object:
15.4 Array Objects
... Every Array object has a length property whose value is always a nonnegative integer less than 232.
Upvotes: 1
Reputation: 22418
There isn't a difference at all programmatically. If you run your example in both chrome developers window and firebug it looks like the 2nd
Name Value
Type
------------- -------------------------------------- ------
selectedItems {...} Object
- [0] "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3" String
Length is an implied property
EDIT
var retVal = 'test';
var params =
{
jobId : 1,
targetLocationIdList : retVal.split(',')
};
console.log(params.targetLocationIdList.length) // prints 1
The code above prints 1 in IE8,Firefox,Chrome (in their dev tools or firebug) so think that this must be an issue with Visual Studio or with Atlas in the way that it shows the object.
Upvotes: 3
Reputation: 498992
In the first instance, you are declaring selectedItems as an array and then assigning to it.
In the second instance, you are assigning to it directly from the split method.
I can only assume that VS takes the array declaration as a hint to display the length property.
Upvotes: 2
Reputation: 14619
How do you know the call fails? Make sure you run in debug mode, or use a failed handler to be notified of the reason.
Upvotes: 0
Reputation: 8218
Might this be a bug in the debugger? (Or is this causing problems in the browser?)
Upvotes: 2
Reputation: 99345
What browser are you using, and are you sure there is no length
property? I just did
var arr = "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3".split(",")
in my Firebug console, and arr
does have a length
property equal to 1, as expected.
Upvotes: 0