Welcome! Log In Create A New Profile

Advanced

Re: Variable scope in javascript module

Dmitry Volyntsev
October 30, 2018 01:10PM
On 30.10.2018 19:58, alweiss wrote:
> Here is a sample that works with Java but not with njs :
>
> function my() {
> resp = "Start";
> console.log ('Initial resp is ' + resp);
>
> function done() {
> resp += "AndContinue";
> console.log('In loop resp is ' + resp)
> }
>
> done();
> }
> resp = 'empty'
> my();
> console.log('End Resp is : ' + resp)
>
> With java :
>
> root@linux3:/etc/nginx# js test.js
> Initial resp is Start
> In loop resp is StartAndContinue
> End Resp is : StartAndContinue
>
>
> With NJS :
>
> root@linux3:/etc/nginx# njs test.js
> ReferenceError: "resp" is not defined in 16
>
>
> Don't know why it doesn't work the same in both runtime.

Currently, njs requires all the variables to be declared.
Adding 'var resp;' at the beginning of the file helps.

docker run -i -t nginx:latest /usr/bin/njs
interactive njs 0.2.4

v.<Tab> -> the properties and prototype methods of v.
type console.help() for more information

>> var resp; function my() { resp = "Start"; console.log ('Initial resp
is ' + resp); function done() { resp += "AndContinue"; console.log('In
loop resp is ' + resp)}; done()}; resp = 'empty'; my(); console.log('End
Resp is : ' + resp)
'Initial resp is Start'
'In loop resp is StartAndContinue'
'End Resp is : StartAndContinue'
undefined
>>





>
> Thanks
>
> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,281699,281747#msg-281747
>
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
>
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Variable scope in javascript module

alweiss October 26, 2018 02:16AM

Re: Variable scope in javascript module

Dmitry Volyntsev October 26, 2018 05:04AM

Re: Variable scope in javascript module

alweiss October 30, 2018 12:07PM

Re: Variable scope in javascript module

alweiss October 30, 2018 12:58PM

Re: Variable scope in javascript module

Dmitry Volyntsev October 30, 2018 01:10PM

Re: Variable scope in javascript module

Valentin V. Bartenev October 30, 2018 01:12PM

Re: Variable scope in javascript module

alweiss October 30, 2018 05:55PM

Re: Variable scope in javascript module

Valentin V. Bartenev October 30, 2018 06:28PM

Re: Variable scope in javascript module

Maxim Konovalov October 30, 2018 06:32PM

Re: Variable scope in javascript module

alweiss October 30, 2018 07:22PM

Re: Variable scope in javascript module

alweiss October 30, 2018 06:47PM

Re: Variable scope in javascript module

Valentin V. Bartenev October 30, 2018 07:58PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 74
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready