javascript - const within block scopes in Node and Chrome (V8) -
i writing nodejs (v4.2.4) app encountered odd behaviour.
function a(number) { this.number = number; } for(var = 0; < 3; i++) { const = new a(i); console.log(a.number); } const b = new a(99); console.log(b.number);
my intuition, coming java (and 1 of firefox), output should have been
0 1 2 99
however, node (and chrome) give me
0 0 0 99
i investigated , understood msn - block scope var
not have block scope in javascript. looking further, msn - const describes const declaration having block scope though:
constants block-scoped, variables defined using let statement. value of constant cannot change through re-assignment, , can't redeclared.
in case node , chrome (in case v8), have kind of shady handling of const
in block scopes. write
this declaration creates constant can either global or local function in declared.
does mean v8 puts every const
declaration global variable while firefox creates local one? how can crucial implemented differently?
v8 requires use strict
directive enable behaviour. has changed in latest version: http://v8project.blogspot.ru/2016/01/v8-release-49.html
quote:
release 4.9 makes block level constructs such class , let available outside of strict mode
this should apply const
Comments
Post a Comment