Java Reference
In-Depth Information
The following code shows you how to pass arguments to the loadWithNewGlobal()
function. The code simply prints the passed arguments:
// Create a script object prints the passed arguments
var scriptObject = {
name: "myscript",
script: "for each(var arg in arguments) {print(arg)}"
};
// Load and evaluate the script object passing three argumsnts
loadWithNewGlobal(scriptObject, 10, 20, 30);
10
20
30
You are not limited to passing only primitive values to the new global scope. You can
also pass functions. You can communicate between two global scopes using functions.
The following code passes a callback function to the new global scope. Notice that
when the passed function is called from the new global scope, the function still uses the
variable x defined in its creator global scope, not the new global scope:
var x = 100;
// Create a script object that prints the passed arguments
var scriptObject = {
name: "myscript",
script: "var x = 200; print('Inside new global. x =', x); " +
"arguments[0]();"
};
// Load and evaluate the script object passing a function object
loadWithNewGlobal(scriptObject, function () {
print("Called back from new global. x = " + x);
});
Inside new global. x = 200
Called back from new global. x = 100
 
Search WWH ::




Custom Search