Given the following javascript snippet in scope:
var x = 10;
function sayx() {
alert(x);
}
sayx();
Of course, you expect the message β10β to appear in the message window, you can perform multiple nesting functions to interact with the way βxβ is defined, because when you allow x to be an environment, a chain of areas approaches.
You can even run the βrecompileβ level with eval to enter new areas at runtime. For example:
var x = 10;
function sayx() {
alert(x);
}
function wrap(f) {
return eval('(function() { var x = 20;(' + f + ')(); })');
}
wrap(sayx)();
, toString, . , , , x.. "20", "10".
, , , 'f' . , , .
, , ? - :
function withScope(f, scope) { ??? }
---
var x = 10, y = 10;
function dothemath() {
alert(x + y);
}
var haxthemath = withScope(dothemath, { x: 9000 });
haxthemath();
, "", , , , , ( ), , .
, .
.
, , , "withScope", :
Window - window properties
Object - { var x = 10 }
Object - { var y = 5 + x }
, + , , :
withScope(somefunc, { foo: 'bar' })
Window - window properties
Object - { var x = 10 }
Object - { var y = 5 + x }
Ext scope - { foo = 'bar' }
, .