I am creating a new project called "Burn". This will be similar to Khan Academy's Computer Science program. How could I have a "private" JavaScript context that cannot use any methods other that those specified?
Asked
Active
Viewed 129 times
1
-
1Hi user, I'd suggest editing your post to clarify *why* you want to do this. At the minimum, it satisfies curiosity, and at most, it may give answerers information that is essential in giving you a good answer. Hope this helps! :) – jamesmortensen May 31 '13 at 02:08
-
@jmort253 Actually he did include that information ("This will be similar to Khan Academy's Computer Science program") – tckmn May 31 '13 at 02:08
-
1@Doorknob - Okay, perhaps. But still, a little more context would make this a more complete, standalone question and not drive users in the other direction, *away* from Stack Overflow. :) – jamesmortensen May 31 '13 at 02:09
-
3Previously asked, and it's impossible: T. J. Crowder's answer to "[Is there a way to jail in Javascript, so that the DOM isn't visible](http://stackoverflow.com/questions/2673695/is-there-a-way-to-jail-in-javascript-so-that-the-dom-isnt-visible)" explains how any such context can be escaped. – May 31 '13 at 02:13
1 Answers
1
You can use IIFE
(immediatelyinvoked function expression) and write your code in it. You can redefine window
object in that context so you cannot access other methods on window object.
(function (window){
//code and other functions here
// here window is undefined
})();
But I am not sure why you'd want that.
See jsFiddle

U.P
- 7,357
- 7
- 39
- 61
-
That would disallow `window.open` but it doesn't say anything about disallowing `open` (think about scope rules, `open` is already in scope). I actually thought it might work, too, until I played with JsFiddle and realized what was happening. I wonder if, though, with some of the newer flavors of ECMAScript if there isn't some variation of this that might work? I dunno. – JayC May 31 '13 at 02:20
-
-
1of course, I suppose there is the option of adding *all* of those `window.things` as parameters to your function. It might work, but *ugh*. – JayC May 31 '13 at 02:25
-
To prevent access, you need to create local variables to shadow all the standard window properties **and** run in strict mode, otherwise it's trivial to access the global/window object regardless of whether a local *window* variable has been created to shadow it. Do `for..in` on `window`, there are a lot of properties and methods. – RobG May 31 '13 at 02:28
-
@RobG: with exception of using `new Function`, how would you go about declaring names of variables in a `for-in` loop? – Qantas 94 Heavy May 31 '13 at 04:45
-