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
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.
The best way is what Pointy described, or alternatively you could just do this:
if ( window[iCheckFcn](itemSrc) ) {
alert("it's a favorite");
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With