[icinga-users] var and locals keywords

Kai Nothdurft Kai.Nothdurft at Rantzau.de
Thu Nov 23 09:21:42 CET 2017



The *local* scope contains variables which only exist during the
invocation of the current function, object or apply statement. *Local*
variables can be declared using the var keyword:

 function multiply(a, b) {
   var temp = a * b
   return temp

Each time the multiply function is invoked a new temp variable is used
which is in no way related to previous invocations of the function.

When setting a variable which has not previously been declared as local
using the var keyword the *this* scope is used.
The *this* scope refers to the current object which the function or
object/apply statement operates on.

 object Host "localhost" {
   check_interval = 5m

In this example the this scope refers to the *localhost* object. The
check_interval attribute is set for this particular host.
You can explicitly access the *this* scope using the this keyword:

 object Host "localhost" {
   var check_interval = 5m

   /* This explicitly specifies that the attribute should be set
    * for the host, if we had omitted `this.` the (poorly named)
    * local variable `check_interval` would have been modified
   this.check_interval = 1m

Similarly the keywords *locals* and *globals* are available to access
the local and global scope.     <----------------------------- !!!!!

- Chapter "Variable Scopes".


>>> On 22.11.2017 at 18:59, Tobias Koeck <tobias.koeck at gmail.com>
> Hi,
> what is the difference between
> var varname = 1
> and
> locals.varname = 1
> And when use one and the other?
> Greetings
> Tobias

More information about the icinga-users mailing list