From Documentation

Jump to: navigation, search




Scope for Each ID space

Java interpreter (BeanShell) is a typical multi-scope interpreter. It creates an interpreter-dependent scope for each ID space. And it's hierarchical. If a variable can't be found in current id space, it will go further to parent's id space try to resolve the variable. For example, two logical scopes are created for window[1] A and B, respectively in the following example. Therefore, var2 is visible only to window B, while var1 is visible to both window A and B in the following example.

<window id="A">
    <zscript>var1 = "abc";</zscript>
    <window id="B">
        <zscript>var2 = "def";</zscript>
    </window>
</window>

Notes

  1. Built in id space owner includes window, page and regular macro.

Declare local variable

In additions, you shall use local variables if possible. A local variable is declared with the class name, and it is visible only to a particular scope of zscript codes. Furthermore, you can make a local variable invisible to EL expressions by enclosing it with {} as follows.

You can see how {} and class name as Date affect scope and EL in the following example.

<window>
	<zscript>
	{
	    Date now = new Date();
	    abc ="def";
	}
	</zscript>
	1:${abc}
	2:${now}
</window>

The result shows: 1:def 2: . abc is visible, and now is invisible.

Please refer to Beanshell's manual and search "scoping"、"local" for more information.




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.