Java Interpreter (BeanShell)"
Maya001122 (talk | contribs) m (Created page with '{{ZKDevelopersGuidePageHeader}} === Scope for Each ID space === Java interpreter (BeanShell) is a typical multi-scope interpreter. It creates an interpreter-dependent scope for …') |
m (correct highlight (via JWB)) |
||
Line 2: | Line 2: | ||
=== Scope for Each ID space === | === 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<ref>Built in id space owner includes < | + | 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<ref>Built in id space owner includes <code>window</code>, <code>page</code> and <code>regular macro</code>.</ref> <code>A</code> and <code>B</code>, respectively in the following example. Therefore, <code>var2</code> is visible only to window <code>B</code>, while <code>var1</code> is visible to both window <code>A</code> and <code>B </code>in the following example. |
<source lang="xml" > | <source lang="xml" > | ||
Line 17: | Line 17: | ||
=== Declare local variable === | === 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 < | + | 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 <code>{}</code> as follows. |
− | You can see how < | + | You can see how <code>{}</code> and class name as <code>Date</code> affect scope and EL in the following example. |
<source lang="xml" > | <source lang="xml" > | ||
Line 34: | Line 34: | ||
</source> | </source> | ||
− | The result shows: < | + | The result shows: <code>1:def 2: </code> . <code>abc</code> is visible, and <code>now</code> is invisible. |
Please refer to [http://www.beanshell.org/manual/bshmanual.pdf Beanshell's manual] and search "scoping"、"local" for more information. | Please refer to [http://www.beanshell.org/manual/bshmanual.pdf Beanshell's manual] and search "scoping"、"local" for more information. |
Latest revision as of 10:38, 19 January 2022
This documentation is for an older version of ZK. For the latest one, please click here.
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
- ↑ Built in id space owner includes
window
,page
andregular 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.