JavaScript Isolated execution context in JavaScript isolated execution context

I'm trying to execute a piece of code within an empty isolated execution context in JavaScript. In the below sample, I'm trying isolate isolated execution scope. What I want to do is to execute a function in context where no global variables are in.

(function() {
  'use strict';

  var scope = Object.create(null);
  var isolated = function() {
    'use strict';
    console.log(document); // Trying to get undefined
                           // but traces `document`.
  };

  isolated.call(scope);
})();

I thought it was simple to nullify global variables but there are too many!

var isolated = function(window, document, location /* etc */) {
  // ...
};

isolated.call(scope, undefined, undefined, undefined /* etc */);

Is there a better way to do this?

Answer:1

There is no good way to do this within javascript itself (but see Gareth Hayes answer for another option).

There are a couple of bad ways.

(function() {
  var scope = Object.create(null);
  var obscurer = {};
  for (var key in this) {
     obscurer[key] = undefined;
  }

  with (obscurer) {
    var isolated = function() {
      'use strict';
      console.log(document);
    };
  }

  isolated.call(scope);
})();

Note that you'll actually get an error because console is not defined rather than document, although you can fix this by not blocking 'console' in the obscurer object. You'll probably find that you need a whole bunch more globals than you realised.

You're also only blocking the enumerable properties of window. If you become aware of nonenumerable properties that you want to block too, you'll have to add those to obscurer.

Of course, using with means you can't use strict mode any more as well, and everyone will look down their noses at you..

There are more interesting options available if you are working within node rather than the browser.

Answer:2

Use my MentalJS parser to isolate the environment. You can then choose which objects/variables it has access to by customizing the code.

http://businessinfo.co.uk/labs/MentalJS/MentalJS.html

http://code.google.com/p/mentaljs/

By default it allows access to document but you can prevent this, customize the environment here http://code.google.com/p/mentaljs/source/browse/trunk/MentalJS/javascript/Mental.js#260 you can then choose if they have access to Math etc.

Answer:3

How can I access the binary representation with JavaScript from a file uploaded with a file input?: <input type="file" name="file"> I can access the details of the uploaded file successfully ...

How can I access the binary representation with JavaScript from a file uploaded with a file input?: <input type="file" name="file"> I can access the details of the uploaded file successfully ...

  1. file input value html
  2. file input value jquery
  3. file input value javascript
  4. file input value react
  5. file input value
  6. file input value fakepath
  7. file input value php
  8. file input value clear
  9. file input value display
  10. input file value set
  11. input file value reset
  12. input file value default
  13. input file value change
  14. input file value base64
  15. input file value laravel
  16. input file value change event
  17. input file value url
  18. input file value empty
  19. input file value get
  20. input file value angular

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the ...

I have an issue about repeatly data after post with ajax. I have buttons without id only data attributes. I use dataTable library. After click, I call a modal with form. There are two fields in the ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

I need to check if at least one element in an array pass a condition. The condition depends on a variable. For example, I'm using something like this: function condition(previousValue, index, array) ...

  1. javascript some method
  2. javascript array some method
  3. javascript object some method

I trying to add click() event for dynamically created button like this HTML <div></div> <button>generate new element</button> Jquery $("button").click(function() { $("...

I trying to add click() event for dynamically created button like this HTML <div></div> <button>generate new element</button> Jquery $("button").click(function() { $("...

  1. click event dynamically created button jquery
  2. jquery click dynamically created element
  3. click on dynamically created button
  4. click function on dynamically created element
  5. click event on dynamically created element jquery
  6. jquery bind click dynamically created element
  7. click event for dynamically created button javascript
  8. click function for dynamically created button jquery
  9. click function on dynamically created element jquery
  10. click event for dynamically created button in asp.net c#
  11. click event for dynamically created li in jquery
  12. click event for dynamically created button c#
  13. trigger click on dynamically created element