带有参数的函数的Javascript eval()

FFi*_*ish 7 javascript arguments eval

我该怎么做呢?

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
Run Code Online (Sandbox Code Playgroud)

Poi*_*nty 8

首先不要使用eval().

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

  if (iCheckFcn(itemSrc)) { alert("it's a favorite"); }
}
Run Code Online (Sandbox Code Playgroud)

函数是对象,您可以将函数的引用分配给任何变量.然后,您可以使用该引用来调用该函数.