0
function showConfirm(reArray) {

    var theHTML = '';
    var optionArray = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7"]; 
    var myButtons = {};
    var j = 1;

    for(var i = 0; i < reArray.length; i++){

                theHTML  +='<div style="text-align:center">' 
                         + '<span>'+j+'.</span>&nbsp;&nbsp;&nbsp;&nbsp;'
                         + '<span>'+reArray[i].RoadNo+'</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
                         + '<span>'+compass_image(reArray[i].Bearing)+'</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
                         + '</div><br/>'    

                j++;
        }

    for(i = 0; i < reArray.length; i++){

    ERROR HERE -----> var placeFunction = function(reArray[i]){

                                plotRoadInfo(reArray[i]);
                                $(this).dialog("close");
                            };

                            myButtons[optionArray[i]] = placeFunction;
    }

    $( "#dialog-modal" ).dialog({
      height: 300,
      modal: true,
      buttons: myButtons

    });

    $('#multipleRE').append(theHTML);
}

So the function gets passed an object array (reArray), it then creates an array of buttons (myButtons) for a jquery dialog box. I'm attempting to to pass reArray[i] to the function that will be used by each button, which is to execute plotRoadInfo(reArray[i]);

I keep getting "Unexpected token [", and I can't figure out why for the life of me.

1
  • You're function clause is a function instantiation. It doesn't make sense for a formal parameter to have an index expression like that. Commented Apr 15, 2013 at 1:34

3 Answers 3

3

You don't specify the type of a parameter in JavaScript, simply use the name and then use it as an array in your function.

var placeFunction = function(reArray){
    plotRoadInfo(reArray[i]);
    $(this).dialog("close");
};

I don't think you even need to specify it though as you're referring to as array that the inner function has access to.

var placeFunction = function() {
    plotRoadInfo(reArray[i]);
    $(this).dialog("close");
};

As Pointy notes this will run in to issues with the i variable being shared so each function will refer to reArray[reArray.length] because the loop will complete when i === reArray.length. A common way to get around this is to call a function that accepts i as a parameter that will return a function.

var placeFunction = (function (item) {
    return function() {
        plotRoadInfo(item);
        $(this).dialog("close");
    };
}(reArray[i]));

Read up on closures for a deeper understanding of how this works. Here is another example of this happening.

Sign up to request clarification or add additional context in comments.

6 Comments

This probably won't work, however, because "i" in the external context is shared among all the "placeFunction" instances.
Thanks for the quick response. I'll try your first suggestion now. I've tried the second suggestion (no specification) and none of the objects properties were passing to plotRoadInfo(). Lemme get on the other option now and I'll get back to you.
Ok so, @DanielImms, passing function(reArray) gets rid of the error, but none of the object properties pass. For example, in plotRoadInfo();: reArray.Lat now comes up with the error Cannot read property 'Lat' of undefined.
I updated my answer, it's undefined because its referring to reArray[reArray.length].
Should I just pass plotRoadInfo(reArray) instead of passing [i], or do I still need to specify that I'm passing the [i]th object?
|
2

The syntax you have is incorrect.

I believe what you want to do is the following:

var placeFunction = 
    (function(arrayitem){
        return function(){
                   plotRoadInfo(arrayitem);
                   $(this).dialog("close");
               };

     })(reArray[i]);

myButtons[optionArray[i]] = placeFunction;

Comments

1

Why does the parameter have an index?

function(reArray[i])

did you mean

function(reArray)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.