views:

30

answers:

2

How do I do this?

function myUIEvent() {
    var iCheckFcn = "isInFavorites";
    var itemSrc = ui.item.find("img").attr("src");

    if (eval(iCheckFcn(itemSrc))) { alert("it's a favorite"); }

function isInFavorites(url) { return true; } // returns boolean
+6  A: 

Don't use eval(), first of all.

function myUIEvent() {
  var iCheckFcn = isInFavorites;
  var itemSrc = ui.item.find("img").attr("src");

  if (iCheckFcn(itemSrc)) { alert("it's a favorite"); }
}

Functions are objects, and you can assign a reference to a function to any variable. You can then use that reference to invoke the function.

Pointy
cheers! thanks for the explanation too.
FFish
+1  A: 

The best way is what Pointy described, or alternatively you could just do this:

if ( window[iCheckFcn](itemSrc) ) {
  alert("it's a favorite");
};
Ashley Williams