Java Interpreter (BeanShell)"

From Documentation
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 <tt>window</tt>, <tt>page</tt> and <tt>regular macro</tt>.</ref> <tt>A</tt> and <tt>B</tt>, respectively in the following example. Therefore, <tt>var2</tt> is visible only to window <tt>B</tt>, while <tt>var1</tt> is visible to both window <tt>A</tt> and <tt>B </tt>in the following example.
+
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 <tt>{}</tt> as follows.
+
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 <tt>{}</tt> and class name as <tt>Date</tt> affect scope and EL in the following example.
+
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: <tt>1:def 2: </tt> . <tt>abc</tt> is visible, and <tt>now</tt> is invisible.
+
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


Stop.png 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

  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.



Last Update : 2022/01/19

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