2

Ok, I don't know how to actually ask this question without showing it. (Also explains why I can't figure out how to search for it on Google.)

Given the following like of code:

dijit.byId('leftNavigationPane').onLoad = function(){setSecondaryNav(url.secondaryNavId);};

I want the variable url.secondaryNavId to be evaluated, so what I really want is it to be treated something like this:

dijit.byId('leftNavigationPane').onLoad = function(){ setSecondaryNav('item1'); };

I am sure there is probably a better way to do this, so feel free to suggest something.

2 Answers 2

5

Don't use eval!

You can use a self-invoking function and closures as follows:

dijit.byId('leftNavigationPane').onLoad = function(id){
    return function(){ setSecondaryNav(id); };
}(url.secondaryNavId); 

This will execute the outer anonymous function immediately (at runtime), passing the url.secondaryNavId parameter, which will then create a closure that the inner anonymous function will use (so id will always contain the assignment-time value of the url.secondaryNavId property).

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

2 Comments

Thank You! Javascript is still a mystery to me at times.
Yeah, I didn't understand these as much until recently, either.
-1

There is the JavaScript eval() function.

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.