views:

151

answers:

2

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.

+2  A: 

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();
}
ZJR
otherwise (that is, if you're using the javascript `with(object) {...}` or you're in a *function scope*) you're supposed to search all the objects that could contain it, in sequence.
ZJR
fyi you could use "this" rather than "window"
Jason S
javascript function variables, IIRC, are function properties, so if you're doing: `function x() {var a=10;}` you will have to check for `varName='a'; typeof x[varName];`
ZJR
`this` makes no sense here, if we're talking about variables and not properties of arbitrary objects. It would still work for plain function calls where `this===window`, but why would you do that?
bobince
`typeof x[varName]` makes no sense here either, we are not talking about a function property.
Crescent Fresh
Only global variables are properties of the global object. In other contexts, variables are **not** properties.
Alsciende
+1  A: 

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.

bobince
It's not exactly the same test. If the variable was declared but its value was _undefined_, then typeof would return "undefined", but in window would return true.
Alsciende
@Alsciende I can see the difference. In the case of this class it would not matter.