Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consts defined in loop blocks retain their values between iterations and after the loop #1310

Closed
michbarsinai opened this issue Mar 6, 2023 · 1 comment

Comments

@michbarsinai
Copy link

Given the following code, Rhino and Deno print different outputs. (the out function is just a compatibility layer between the two engines):

function out(x){
    if ( typeof print != "undefined" ) {
        print(x);
    } else {
        console.log(x);
    }
}

let loopCount=3;
while ( loopCount-- ) {
   const i=loopCount;
   out(i);
}

// out(i);

Rhino prints:

$ java -jar rhino-1.7.14.jar  /Users/michael/sandbox/sm1/spec/js-dis/const.js
2
2
2

While Deno prints:

$ deno run /Users/michael/sandbox/sm1/spec/js-dis/const.js
2
1
0

Might be useful to note that when the last line is un-commented, Deno complains that i is not defined at that line, whereas Rhino prints 2. However, if i is defined as let, Rhino behaves like Deno, and complains that i is not defined.

Seems like Rhino defines consts on a parent scope of the correct scope (maybe where vars would have been defined?), but I'm no expert in Rhino's internals.

@p-bakker
Copy link
Collaborator

p-bakker commented Mar 7, 2023

Duplicate of #939

@p-bakker p-bakker marked this as a duplicate of #939 Mar 7, 2023
@p-bakker p-bakker closed this as completed Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants