Frostorm

[]D[][]V[][]D
Regular
Joined
Feb 22, 2016
Messages
2,788
Reaction score
2,236
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
Regular
Joined
Mar 16, 2012
Messages
7,534
Reaction score
12,008
First Language
Indonesian
Primarily Uses
N/A
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

Regular
Regular
Joined
Oct 20, 2015
Messages
3,368
Reaction score
2,571
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
Regular
Joined
Feb 22, 2016
Messages
2,788
Reaction score
2,236
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

Regular
Regular
Joined
Jun 25, 2021
Messages
1,243
Reaction score
1,930
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
Regular
Joined
Jan 12, 2019
Messages
1,624
Reaction score
1,162
First Language
Polish
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

Regular
Regular
Joined
Oct 20, 2015
Messages
3,368
Reaction score
2,571
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 Posts

Latest Profile Posts

Updated my game's development post with the first video of it in action!


'Milestone Get!' :p
Heh, heh. I've been experimenting with a naughty word detector Common Event and using scripts to check certain strings. I never thought I would see such language in computer code. I would post a screen shot, but it probably wouldn't be appropriate, even with a Spoiler. But it certainly makes me laugh and it works great.

Forum statistics

Threads
136,806
Messages
1,270,228
Members
180,564
Latest member
mans931
Top