1

I am creating a variable where its value is determined by a particular method. This particular method should return an object with two properties. However, after the method returns the variable is undefined. I check the objects value right before it's returned and its fine. So there is something happening between the return and instantiate of the variable that causes it to be undefined. Here is a code snippet:

var results = findTarget(target, after, append); //undefined

function findTarget(target, after, append){
    var currenttemplate = $(target).attr('data-template');

    for(var i=0; i<after.length; i++){
        if(after[i] === currenttemplate)
            return {target : target, drop : "after"};
    }

    for(var j=0; j<append.length; j++){
        if(append[j] === currenttemplate){
            var obj = {target : target, drop : "append"};
            console.log(obj); //is fine here
            return obj; //this gets returned
        }
    }

    if(currenttemplate === threshold) {
        return "";
    }

    findTarget($(target).parent()[0], after, append); 
}
0

1 Answer 1

3

You need to add a return at the end:

function findTarget(target, after, append){
    /* ... */
    return findTarget($(target).parent()[0], after, append); 
}

If not, you call recursively findTarget, and this recursive call returns the appropriate value, but the first call to findTarget doesn't return it.

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

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.