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

发布于 2024-08-28 07:21:24 字数 2569 浏览 5 评论 0原文

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

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

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

也许这很简单,但我不知道如何做。

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

Edit2:我又看了一遍,也许我向您展示更大的图片会更好。我很高兴专家们解释得如此漂亮,最好有更多代码:

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();

Edit3:

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

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

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

How to write this JavaScript code without eval?

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

The point is that the name of the var that I want to check for is in a string.

Maybe it is simple but I don't know how.

Edit: Thank you for the very interesting answers so far. I will follow your suggestions and integrate this into my code and do some testing and report. Could take a while.

Edit2: I had another look at the could and maybe itis better I show you a bigger picture. I am greatful for the experts to explain so beautiful, it is better with more code:

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();

Edit3:

I am planning to put this in the global namespace in variable MYNAMESPACE.loadCheck, for simplicity, so the result would be, combining from the different answers and comments:

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

Of course I will have to update the Loader class where ever "varName" is mentioned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-09-04 07:21:24

在 JS 中,每个变量都是一个属性,如果您不知道它是谁的属性,那么它是一个 window 属性,所以我想,在您的情况下,这可以工作:

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

in JS every variable is a property, if you have no idea whose property it is, it's a window property, so I suppose, in your case, this could work:

var typeOFString = typeof window[that.modules[modName].varName]
if (typeOFString !== "undefined") {
  doSomething();
}
゛清羽墨安 2024-09-04 07:21:24

由于您只是测试该项目的存在,因此您可以使用in 而不是typeof

因此,对于 ZJR 的答案中的全局变量,您可以在 window 对象上查找它们:

if (that.modules[modName].varName in window) {
    ...
}

如果您需要查找局部变量,则没有 eval 就无法做到这一点。但这将是进一步严重设计失误的迹象。

Since you are only testing for the existence of the item, you can use in rather than typeof.

So for global variables as per ZJR's answer, you can look for them on the window object:

if (that.modules[modName].varName in window) {
    ...
}

If you need to look for local variables there's no way to do that without eval. But this would be a sign of a serious misdesign further up the line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文