I’m struggling to return an array from a function call – code below:
///////////// Generic Functions
function spotJoinPosition() {
var pos = {
offset: $('div#spotJoinSite').offset(),
width: $('div#spotJoinSite').width(),
height: $('div#spotJoinSite').height()
}
return(pos);
}
var positionData = spotJoinPosition();
alert(positionData);
alert(positionData[width]);
When I alert positionData I get [object][object] and then undefined.
Advice?
alert(positionData[width]);This is alerting a key in positionData, and using the variable
widthas the key. You haven’t defined a variable calledwidth, so it’s essentially looking uppositionData[undefined]. What you want ispositionData.width, orpositionData['width'], but there is no reason for quotes here.Quotes would only be required if you had a key with non alphanumeric characters.
positionData['some-key']works, butpositionData.some-keyis a syntax error, because variables cannot have-in them.Also, your code SHOULD be erroring, because width isn’t defined anywhere. I’m worried that you have a globally defined
widthvariable somewhere in your code.