javascript - 如何在没有 eval 的情况下编写这段 JavaScript 代码?

标签 javascript eval

如何在没有 eval 的情况下编写这段 JavaScript 代码?

var typeOfString = eval("typeof " + that.modules[modName].varName);
if (typeOfString !== "undefined") {
  doSomething();
}

重点是我要检查的 var 的名称在字符串中。

也许这很简单,但我不知道怎么做。

编辑:感谢您到目前为止提供的非常有趣的答案。我会按照您的建议并将其集成到我的代码中并进行一些测试和报告。可能需要一段时间。

Edit2: 我又看了一眼 jar 头,也许我给你看一张更大的图画更好。感谢专家们解释的这么漂亮,代码越多越好:

MYNAMESPACE.Loader = ( function() {

  function C() {
    this.modules = {};
    this.required = {};
    this.waitCount = 0;
    this.appendUrl = '';
    this.docHead = document.getElementsByTagName('head')[0];
  }

  function insert() {
    var that = this;
    //insert all script tags to the head now!
    //loop over all modules:
    for (var modName in this.required) {
      if(this.required.hasOwnProperty(modName)){
        if (this.required[modName] === 'required') {
          this.required[modName] = 'loading';
          this.waitCount = this.waitCount + 1;
          this.insertModule(modName);
        }
      }
    }

    //now poll until everything is loaded or 
    //until timout

    this.intervalId = 0;

    var checkFunction = function() {
      if (that.waitCount === 0) {
        clearInterval(that.intervalId);
        that.onSuccess();
        return;
      }
      for (var modName in that.required) {
        if(that.required.hasOwnProperty(modName)){
          if (that.required[modName] === 'loading') {
            var typeOfString = eval("typeof " + that.modules[modName].varName);
            if (typeOfString !== "undefined") {
              //module is loaded!
              that.required[modName] = 'ok';
              that.waitCount = that.waitCount - 1; 
              if (that.waitCount === 0) {
                clearInterval(that.intervalId);
                that.onSuccess();
                return;
              }
            }
          }
        }
      }
    };

    //execute the function twice a second to check if all is loaded:
    this.intervalId = setInterval(checkFunction, 500);
    //further execution will be in checkFunction,
    //so nothing left to do here
  }
  C.prototype.insert = insert;

  //there are more functions here...

  return C;
}());


var myLoader = new MYNAMESPACE.Loader();

//some more lines here... 

myLoader.insert();

编辑 3:

为了简单起见,我计划将它放在变量 MYNAMESPACE.loadCheck 的全局命名空间中,所以结果将是,结合不同的答案和评论:

if (MYNAMESPACE.loadCheck.modules[modName].varName in window) {
  doSomething();
}

当然,我必须更新提到“varName”的 Loader 类。

最佳答案

在 JS 中,每个变量都是一个属性,如果你不知道它是谁的属性,它就是一个 window 属性,所以我想,在你的情况下,这可能有效:

var typeOFString = typeof window[that.modules[modName].varName]
if (typeOFString !== "undefined") {
  doSomething();
}

关于javascript - 如何在没有 eval 的情况下编写这段 JavaScript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2600420/

相关文章:

ios - Swift 中是 ScriptEngine 或类似 eval() 的函数吗?

javascript - Ajax 加载后附加事件与委托(delegate)事件 : what factors to consider?

php - 我可以通过任何方式检查 url 是否指向有效图像(使用 JS/PHP/ZendFramework)?

javascript - `onclick` 使用 Internet Explorer 9 单击元素时未调用事件

javascript - 如何在 iframe 中跟踪用户

javascript - 不使用 eval 创建未知大小的对象

assembly - 组装级别的评估

bash - 找出哪个 shell tcsh/bash/etc。在脚本上使用 eval 时正在运行

python - 如何在不使用 eval() 或 exec() 的情况下创建规则引擎?

javascript - typescript :Getter can't return array with custom type