[icinga-users] var and locals keywords

Tobias Koeck tobias.koeck at gmail.com
Thu Nov 23 13:31:58 CET 2017


Hi Kai,

just checked it out. It's possible to create a local variable with

locals.temp = a * b

instead of

var temp = a * b.

The usage of the keywords locals, this and globals aren't necessary just
for readability. As far as I have read there is a priority from locals to
this to globals variables. If you just write

'something ...' = temp

it will check out the different scopes and gets the first that fits. With
the keyword you can force Icinga 2 to take a specific scope variable.

Greetings
Tobias

On Thu, Nov 23, 2017 at 11:53 AM, Kai Nothdurft <Kai.Nothdurft at rantzau.de>
wrote:

> Hi Tobias,
>
> AFAIK the usage of the keyword "locals" is more or less for readability.
> I'm not sure, if you can create the variable using your second example, but
> you can access it in your function using "locals.temp" afterwards. If you
> have already created variables named "temp" in different scopes, e.g. in
> global and in local scope, Icinga2 will use automatically the variable from
> the local scope, unless you use the "globals" specifier (globals.temp). The
> use of "locals.temp" to access the variable named "temp" in the local scope
> would make it more readable but otherwise makes no difference.
>
> best regards
> Kai
>
>
> >>> On 23.11.2017 at 09:48, Tobias Koeck <tobias.koeck at gmail.com> wrote:
> > 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
>
>
> _______________________________________________
> 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/4d8ac2aa/attachment.html>


More information about the icinga-users mailing list