Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,434
Reaction score
2,033
First Language
English
Primarily Uses
RMMV
Ever since I learned about "let", I was wondering if it would be good practice to use it instead of "var" in the context of Notetags and Lunatic Mode. It doesn't really matter, right? Like, there's no real difference in such use cases, is there?
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,642
Reaction score
8,660
First Language
Indonesian
Primarily Uses
RMVXA
It probably does matter, or maybe not. Using VAR you might end up populating a global variable, while LET is locally scoped. Depending on how the code is executed, it probably doesn't matter.

Either way, try to get used to using LET instead of VAR.
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,575
Reaction score
1,623
First Language
Spanish
Primarily Uses
RMVXA
it's not the same thing.
VAR must take priority over LET, because you cant operate between undefined variables in an explicit environment.

this probably doesn't happen anymore in modern languages, but in old systems that was the case: VAR was the definition, LET was an instruction.
nowadays, it's assumed that any line involving variables with no instruction is an operation.

that actually ties to the fundamental structure of all languages: data structures, instructions, control structures, procedures and objects.
instructions are easy to spot: it's anything usually colored blue in the form of a block, they start with a key word, and end in ;, }, or "end"
control structures are larger blocks and contain several instructions (fixed loops, open loops, single conditionals, case conditionals, etc)
procedures are collections of control structures and instructions, usually returning a value, or returning to a previous line after going out of a main procedure.
objects are larger structures containing all of them, usually saved to files.

"but, what are instructions that *don't* have an instruction, such as straight operations between variables?" you might say.
.....that's exactly what LET was used for.... it used to be an instruction.
 

Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,434
Reaction score
2,033
First Language
English
Primarily Uses
RMMV
@gstv87 So are you advising against using LET? I get they aren't the same, even though there are situations where they can be interchangeable. Just wondering what's considered good practice when using them in Notetags/Lunatic Mode. I mean, the notetags themselves, and thus their contents, are already self-contained right?
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
126
Reaction score
92
First Language
English
Primarily Uses
RMMZ
I can't say that I've ever actually looked at exactly how the plugins implement Lunatic Mode, but I would imagine that they're probably just plugging the code from your note tag into an eval() call. If that's the case, then the answer to your question depends on whether or not the plugin is running in strict mode (i.e. it has 'use strict'; either as the first statement of the plugin or as the first statement in an enclosing function or IIFE).

If the plugin is using strict mode, then it won't matter whether or not you use var or let in your note tag. But if the plugin isn't using strict mode, then the variables that you declare (using var) in your note tag will be scoped to the function that is processing that note tag. In most cases, this probably won't matter, but if that particular function happens to be using a variable with the same name, then it can cause problems. So using let is safer if the plugin isn't using strict mode, which I believe includes Yanfly's plugins (IIRC).

TLDR: Usually won't matter, but it's best to play it safe and use let.
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
1,403
Reaction score
852
First Language
Poland
Primarily Uses
RMMV
The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

JavaScript:
function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar); // Foo Bar

  {
    var moo = "Mooo"
    let baz = "Bazz";
    console.log(moo, baz); // Mooo Bazz
  }

  console.log(moo); // Mooo
  console.log(baz); // ReferenceError
}

run();

The reason why let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.
Example:
JavaScript:
var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
  // and store them in funcs
  funcs[i] = function() {
    // each should log its value.
    console.log("My value: " + i);
  };
}
for (var j = 0; j < 3; j++) {
  // and now let's run each one to see
  funcs[j]();
}
My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable. People had to create immediately invoked functions to capture correct values from the loops but that was also hairy. While variables declared with var keyword are hoisted (initialized with undefined before the code is run) which means they are accessible in their enclosing scope even before they are declared:
JavaScript:
function run() {
  console.log(foo); // undefined
  var foo = "Foo";
  console.log(foo); // Foo
}

run();
let variables are not initialized until their definition is evaluated. Accessing them before the initialization results in a ReferenceError. The variable is said to be in "temporal dead zone" from the start of the block until the initialization is processed.
JavaScript:
function checkHoisting() {
  console.log(foo); // ReferenceError
  let foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

It's worth to mention, at the top level, let, unlike var, does not create a property on the global object and rememeber, In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,575
Reaction score
1,623
First Language
Spanish
Primarily Uses
RMVXA
In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.
this.
that's what I meant, specifically applied to JS.

that's what would have happened in old languages.
 

Latest Threads

Latest Profile Posts

Apparently I'm a grandpa now.

20220128_181216.jpg20220128_181227.jpg
Sales Goal of Learn RPG Maker MV at 64% now! :kaopride:
When I was in the hospital I did actually have a nurse that was funny, did all these extra nice things for me and said she liked me. So you know what that means. That will be the last time I'll hear from her.
Anyone here ever confronted a never-ending loading screen? Took me 3 hours to realize the problem was the font format :yswt:
Ah, the problems a noob runs into...

Forum statistics

Threads
118,803
Messages
1,118,827
Members
155,985
Latest member
messiahAF
Top