[icinga-users] var and locals keywords

Tobias Koeck tobias.koeck at gmail.com
Thu Nov 23 09:48:56 CET 2017


Hi Kai,

thanks for the answer.

I did read the 'manual / website documentation several times and I have
understood the scope of local variables.

It's possible to write the function either with

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

or with

 function multiply(a, b) {
   locals.temp = a * b
   return temp
 }

Both 'creates' the local variable temp if it isn't available? So why use
var if you can use locals.varname, too?

Greetings
Tobias
On Thu, Nov 23, 2017 at 9:21 AM, Kai Nothdurft <Kai.Nothdurft at rantzau.de>
wrote:

> RTFM ;)
>
> <quote>
>
> 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
> instead.
>     */
>    this.check_interval = 1m
>  }
>
> Similarly the keywords *locals* and *globals* are available to access
> the local and global scope.     <----------------------------- !!!!!
> </quote>
>
> Source:
> <https://www.icinga.com/docs/icinga2/latest/doc/17-language-reference/>
> - Chapter "Variable Scopes".
>
> Gruss
>   Kai
>
>
> >>> On 22.11.2017 at 18:59, Tobias Koeck <tobias.koeck at gmail.com>
> wrote:
> > Hi,
> >
> > what is the difference between
> >
> > var varname = 1
> >
> > and
> >
> > locals.varname = 1
> >
> > And when use one and the other?
> >
> > Greetings
> > Tobias
> _______________________________________________
> icinga-users mailing list
> icinga-users at lists.icinga.org
> https://lists.icinga.org/mailman/listinfo/icinga-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.icinga.org/pipermail/icinga-users/attachments/20171123/bd424a35/attachment.html>


More information about the icinga-users mailing list