B4J Question [BANano] The var, let and const battle?

Mashiane

Expert
Licensed User
Longtime User
Ola

var.jpg


I know I should not be worried as BANano is awesome. Whilst it uses var internally for most things (as seen on the generated source code), there has been a move towards the usage of let and const when defining variables above var, due to some scoping issues. Well, we don't have any issues of such, but just curious anyway. Do you mind sharing your views?

Ta!
 

alwaysbusy

Expert
Licensed User
Longtime User
This is mainly to preserve compatibility with B4X. After all, BANano does not try to mimic JavaScript but rather allows you to write normal B4X code (using its syntax rules) without having to worry about JavaScript specific rules. The "Let" scope rules in JavaScript are not that simple, especially when someone is not used to it in B4X.

In B4X, this is allowed for example:
B4X:
Dim x as String = "Hello"
Dim x as String = "Goodbye"

Lucky, in JavaScript var does allow this too:
B4X:
var x = "Hello";
var x = "Goodbye";

Let does not allow that:
B4X:
let x = "Hello";
let x = "Goodbye"; // error, x was already declared

There is also no "Let" keyword in B4X. So that would mean one would have to declare such a variable as something like:
B4X:
BANano.Let("x", "Hello")
' and get it
log(BANano.Get("x"))
' and I wouldn't even know how to use it in a For loop declaration for example without the IDE throwing errors.
which, besides looking silly, breaks with how normal B4X variables are declared. And the B4X IDE would not throw an error if you misused it in scope, because this concept does not exist here.

Const is supported in BANano, in the sense that it does not throw an error when transpiling. You can use it in your B4X code, and it will throw an error in the B4X IDE if you try to change it. Once generated to JavaScript, it is stripped, as it doesn't really matter anymore (nowhere in your code will you try to reassign its value as it was not allowed in the IDE).

"Let" was introduced to solve a problem in JavaScript that does not exist in B4X. And not having it has the advantage it allows BANano also to transpile for ES5 too.

BANano allows B4X coders to write JavaScript in the B4X syntax, which is the point of this whole exercise. ;)

Alwaysbusy
 
Upvote 0
Top