0

I am using an ERP that has the functionality of allowing me to write Javascript in order to control/adjust the flow of events. In my case there is no "window" global object.

I need to use eval() on global scope and I am missing the name of the global object. Is there a way of finding this out?

5
  • 1
    What makes you think you need to "use eval() on global scope"? Commented Apr 29, 2017 at 17:01
  • 1
    Check the documentation or ask the support for whether the global object is exposed through a global variable, we cannot help you without know which ERP you use. Commented Apr 29, 2017 at 17:27
  • if you need to access variables dynamically, you're usually doing something wrong. You should use an object that contains the data you want, you can then access the properties dynamically. Commented Apr 29, 2017 at 17:44
  • What do you need the global object for anyway? Try to avoid global variables and global state as much as you can. Commented Apr 29, 2017 at 18:40
  • @T.J.Crowder I am using eval for developing/testing purposes. The ERP I am using implements a not-so-developer-friendly way of inserting/correcting code. There is about a one-minute overhead to make any change. So while debugging, it is much faster to edit the code and store it in a database field and eval it through a button-click in the main screen rather than do it otherwise. Commented May 2, 2017 at 14:26

1 Answer 1

1

I need to use eval() on global scope...

If you literally mean you need to eval something at global scope, you can do that with indirect eval:

(0, eval)("your code here");

It looks weird, but it makes eval work in global scope rather than local scope.

var n;    // A global `n`
function direct() {
  var n;
  
  eval("n = 'a';");
  console.log("Direct:");
  console.log("  local n =  " + n);
  console.log("  global n = " + window.n);
}
function indirect() {
  var n;
  
  (0, eval)("n = 'b';");
  console.log("Indirect:");
  console.log("  local n =  " + n);
  console.log("  global n = " + window.n);
}
direct();
indirect();

...and I am missing the name of the global object. Is there a way of finding this out?

If you're running your code in loose mode, you can get access to the global object by calling a function, which will run with this referencing the global object. So:

var global = (function() { return this; })();

...gives you a variable called global that references the global object. It doesn't work in strict mode, because in strict mode, this in that function will be undefined.

It's also entirely possible that your code is being called with this already referencing the global object, so you might check that first.

Sign up to request clarification or add additional context in comments.

3 Comments

That's an interesting eval trick. I'm curious how that works. Presumably it's spec-defined behavior and not e.g. a V8 quirk?
@Jordan: Yes, it is as of ES2015, but browsers have supported it for years. Direct eval is described here, indirect eval is described here (that latter is a bit confusing because it seems like it should be the direct version, but the first link explains why you only get there if you're doing it indirectly as in the answer above).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.