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?

Saloom
  • 133
  • 8
  • 1
    What makes you think you need to "use eval() on global scope"? – T.J. Crowder Apr 29 '17 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. – Bergi Apr 29 '17 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. – Barmar Apr 29 '17 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. – Bergi Apr 29 '17 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. – Saloom May 02 '17 at 14:26

1 Answers1

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.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • 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 Running Apr 29 '17 at 17:12
  • 1
    @Jordan: Yes, it is as of ES2015, but browsers have supported it for years. Direct `eval` is described [here](https://tc39.github.io/ecma262/#sec-function-calls-runtime-semantics-evaluation), indirect eval is described [here](https://tc39.github.io/ecma262/#sec-eval-x) (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). – T.J. Crowder Apr 29 '17 at 17:24
  • 1
    See http://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript – Barmar Apr 29 '17 at 17:39
  • @T.J.Crowder Thanks for the spec links! – Jordan Running Apr 29 '17 at 18:49