Variable scope in javascript module

Valentin V. Bartenev vbart at nginx.com
Tue Oct 30 17:09:50 UTC 2018


On Tuesday 30 October 2018 12:58:53 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 at linux3:/etc/nginx# js test.js
> Initial resp is Start
> In loop resp is StartAndContinue
> End Resp is : StartAndContinue
> 
> 
> With NJS :
> 
> root at 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.
> 
[..]

njs implements "strict mode" of JS:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

It's said in the beginning of documentation: http://nginx.org/en/docs/njs/

  wbr, Valentin V. Bartenev



More information about the nginx mailing list