Complete Beginner Exploring rpg_windows.js

So after receiving a lot of information about javascript and reading a whole bunch too, I thought I'd try to understand the code in rpg Maker by asking questions about what I don't understand.

You can read more about it here.

So I opened up the file in NotePad++ and did a bit of snooping around
upload_2019-8-16_14-17-15.png

In the code above, I assume this is the size in pixels. Since that part of the code isn't in a body code { } would I be right to say that those are global as opposed to local?

Thank you!
I don't know the exact naming for javascript, but _variables are variables accessible for the whole object (Window_Base in this case), AND any external object that wants to read them.

global, instanced, whatever the naming.... that's what they are.
as opposed to not-"_"variables, which can't be accessed from the outside, nor from outside the function they're used in.
So you mean you're not sure about the syntax but by the way you describe it, it does mean "Global". I wasn't aware that underscore variables were always Global, thank you!
They're not global, they're not local, they're static. In other words they're associated with the class itself and can be used even without instantiating a new member of the particular class.
Also, it's not decided by the _ thing, but by the fact that they're variables of Window_Base class itself and not of a particular member of the class.
^ Like Poryg says, they're static (i.e. single-value across all Window_Base "clones") because they're defined directly on Window_Base rather than on its prototype.

The underscore is just a convention to indicate that the property is meant to be accessible only by the object that owns the property, but nothing really stops you from accessing such values (like Window_Base._iconWidth) at will. Sometimes there are "getter" functions defined for retrieving these values, e.g.
Code:
Window_Base.prototype.isOpening = function() {
    return this._opening;
};
In a more strictly-typed object-oriented language, the name would be "private property" or "field". It helps to keep the code tidier when you know exactly what values will, or will not, be seen by other objects. 🙂

In RMMV's code, global variables are preceded by a $ symbol, e.g. $dataActors, $gamePlayer, etc. You'll find those defined in rpg_managers.js (and referenced throughout the code), e.g.
Code:
var $dataActors       = null;
The $ prefix is just another naming convention, though; they're global because they're defined by themselves, outside of any {code block}, which causes them to be defined on the global object by default. In RMMV's case, the global object is the game window. Technically everything in the code is a child (or grandchild, etc) of the global object. 🙏
Alright so I went and looked up static on MDN in order to get a better idea of what you guys were talking about.
The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.

So it's not something you'd call on like local and global, it's something else entirely.

The $ prefix is just another naming convention, though; they're global because they're defined by themselves, outside of any {code block}, which causes them to be defined on the global object by default

Question 1:
So using $ is another way to say something is global? I could either put something out of {code block} or use $ or do I have to do both: put them outside of {code block} and use the dollar sign?


Question 2:
index.php

The . there, is it a dot operator? Does that mean that what comes after that dot operator is a method? If so, would that be an instance method or a static method?

EDIT: picture doesn't show, I meant:
Window_Base._iconWidth = 32;

Question 3:
In other words they're associated with the class itself and can be used even without instantiating a new member of the particular class.
I'd like to read more about the concept of class. I searched on MDN but there a few entries. Could someone point me to relevant link (or some other resource somewhere else?).

That's it, I'll try to limit myself to three questions although I'd have a lot more 🙂.
I recommend taking a Java or C++ tutorial instead. These two languages are object oriented and even though the syntax is different, the key principles are the same everywhere.
Question 1:
So using $ is another way to say something is global? I could either put something out of {code block} or use $ or do I have to do both: put them outside of {code block} and use the dollar sign?

In order to declare something as global, you have to make it a property of the global object (which, in JavaScript, is the window object). There are two ways to do so:
Code:
var someGlobalVariable = 'Since this variable is declared outside of any functions using the var keyword, it\'s globally available to all files.';

In JS, whenever you use the "var" keyword to declare a variable, its scope is determined by whatever function it was declared in. If it's not in a function, it's set as a property on the window object.

f58SEOF.png


The other way to do it is to just directly set properties on the window:
Code:
window.someGlobalVariable = 'This is the same thing as declaring "var someGlobalVariable"';

The $ symbol doesn't mean anything as far as JavaScript is concerned, it's just a naming convention for humans. The RMMV developers chose to begin all global variable names with a $ to indicate that it's global, but you could name your global variables whatever you want. Naming conventions and standards make code easier to work with, however, especially when working with other peoples' code - if everyone follows the same set of standards, you spend less time trying to decipher their intent and logic.

Question 2:
index.php

The . there, is it a dot operator? Does that mean that what comes after that dot operator is a method? If so, would that be an instance method or a static method?

EDIT: picture doesn't show, I meant:
Window_Base._iconWidth = 32;

That is a dot operator, which indicates a property of an object. Methods are object properties that are functions - since _iconWidth and _iconHeight and the others are just integers (or, as far as JavaScript is concerned, Numbers), they're just properties of the Window_Base function.

Question 3:

I'd like to read more about the concept of class. I searched on MDN but there a few entries. Could someone point me to relevant link (or some other resource somewhere else?).

JavaScript didn't really have classes until ES6 was introduced, nor did it have static variables, or really private or public properties as most people were used to them. What the RMMV engine uses is the closest thing JavaScript had, which was constructor functions and the prototype. The syntax used by the RMMV engine is more like applying Java/Ruby concepts to a different language and forcing design principles in a way that's very strange or uncommon for JS.

Often when other developers around here talk about classes, private and public properties, and static vs. instanced properties, they're using language and concepts that were common to other languages that JavaScript didn't pick up until fairly recently (and that RMMV's JavaScript has by concept, but not by name, as a general rule).

Poryg's suggestion of finding some resources for object-oriented programming with Java or C++ would be a good way to familiarize yourself with those concepts - although JS does now have the capability to support classes, statics, etc., those keywords aren't used in the RMMV engine because it wasn't written with ES6 support in mind.
I'd be dishonest to say I'm pumped to do a C++ course although that'd be the best course of action. I'll look around if I can find something. I actually did experiment a bit with Java beforehand. I don't mind learning little by little. Whatever I learn is a step forward.

The $ symbol doesn't mean anything as far as JavaScript is concerned, it's just a naming convention for humans. The RMMV developers chose to begin all global variable names with a $ to indicate that it's global, but you could name your global variables whatever you want. Naming conventions and standards make code easier to work with, however, especially when working with other peoples' code - if everyone follows the same set of standards, you spend less time trying to decipher their intent and logic.

This I find very interesting to read and learn. Thank you.

That is a dot operator, which indicates a property of an object. Methods are object properties that are functions - since _iconWidth and _iconHeight and the others are just integers (or, as far as JavaScript is concerned, Numbers), they're just properties of the Window_Base function.

Objects are something I was fuzzy about. For some reason I was always reluctant to look it up properly as I anticipated I wouldn't get it. So I decided to jump straight into it.

So to quote this website:
These objects are quite different from JavaScript’s primitive data-types(Number, String, Boolean, null, undefined and symbol) in the sense that while these primitive data-types all store a single value each (depending on their types).

So right from the get go we know objects and primitives are mutually exclusives, i.e. objects are not primitives.

From a diagram perspective:
upload_2019-8-17_16-36-0.png

where objects are essentially more complex than the primitives (which in turn are the 7 base types of data).

JavaScript didn't really have classes until ES6 was introduced, nor did it have static variables, or really private or public properties as most people were used to them. What the RMMV engine uses is the closest thing JavaScript had, which was constructor functions and the prototype. The syntax used by the RMMV engine is more like applying Java/Ruby concepts to a different language and forcing design principles in a way that's very strange or uncommon for JS.
So what you're saying is that the current code of RMMV isn't up to the standards of ES6 if I understand correctly.
I'd be dishonest to say I'm pumped to do a C++ course although that'd be the best course of action. I'll look around if I can find something. I actually did experiment a bit with Java beforehand. I don't mind learning little by little. Whatever I learn is a step forward
I always recommend C++ not to make people uncomfortable, but because C++ online courses have Object oriented programming chapter, which would explain what static and object means. Core programming principles are the same for every language, it's just the syntax and way of implementation that is different.

As for your diagram, since you said yourself that objects and primitives are mutually exclusives, the diagram would contain no intersections between the two circles. Nevertheless you are correct, objects are not primitives and as such
Code:
var x = 5;
y = x;
y = 7;
console.log (x);
and
Code:
var x = {prop: 5};
var y = x;
y.prop = 5;
console.log(x.prop);
will throw different results.

So what you're saying is that the current code of RMMV isn't up to the standards of ES6 if I understand correctly.
Yeah, current code is written in ES5 standards and will not upgrade, because they want to ensure compatibility with as many plugins as possible.
I always recommend C++ not to make people uncomfortable, but because C++ online courses have Object oriented programming chapter, which would explain what static and object means. Core programming principles are the same for every language, it's just the syntax and way of implementation that is different.
Oh I didn't think that you did to make me uncomfortable. Right now I'm learning at a steady pace, a week ago I didn't know anything about JavaScript at all. If I'm ever stumped and disappointed about my progress, I'll take your suggestion for sure.

Yeah, current code is written in ES5 standards and will not upgrade, because they want to ensure compatibility with as many plugins as possible.
Ah that's interesting, makes sense.

As far as static is concerned, here's my take on it.

From what I can read online, static vs dynamic programming relates to run-time although I wouldn't be able to get into the specifics to be quite honest.

It's probably not what you meant however. As far as object based programming I would assume it's the following:
upload_2019-8-17_17-41-40.png

So from the get-go I would imagine that JavaScript is a language which revolves around functions.

In this article, we are concerned with only Inheritance and Encapsulation since only these two concepts apply to OOP in JavaScript, particularly because, in JavaScript, objects can encapsulate functionalities and inherit methods and properties from other objects.

This is something caethyril told me about before. I couldn't understand it back then which is likely because I didn't have any programming background.

According to the article, inheritance seems to mean to reuse code in some way or another.

To go slightly off-track, this I find surprising:
upload_2019-8-17_17-49-41.png

I thought strings and numbers were primitives and therefore not objects as we previously discussed:
upload_2019-8-17_17-50-57.png

I'll stop here. I don't expect you guys to explain the difference between static and dynamic. An explanation about this apparent contradiction I've quoted would be nice however.

@Zevia : I haven't forgotten about the rest of your message and will get to it (eventually!).
I'll stop here. I don't expect you guys to explain the difference between static and dynamic. An explanation about this apparent contradiction I've quoted would be nice however.
It is not a contradiction. Javascript Objects are like data lists, dictionaries in Python or structs in C like languages. But essentially even non-Objects in js are object in programming sense, because every variable in js can have properties/attributes and member functions.
Inheritance has to do with parent & child classes. A dog is a land mammal, so it has legs. But some mammals have two legs, some have 4. That's the whole point of inheritance.
In JavaScript, objects are created using what are known as constructor functions. While they don't have to be capitalized, common practice is to do so in order to indicate that they are constructor functions. What JavaScript calls a constructor function, many other languages call classes. Usually when people reference something like "The Window_Base class", what they mean in JavaScript is "The Window_Base constructor function".

The most basic constructor function in JavaScript is a function called Object. Almost every time you generate a value in JavaScript, you're getting an Object of some sort. Other languages would call it the Object class.

A constructor function is called that because you construct "instances" of something using that function. For example, if you want two Objects, you could say:
Code:
var firstObject = new Object();
var secondObject = new Object();

You now have two "instances" of the Object constructor - and you can have them take diverging paths in terms of what properties they could have. For example:
Code:
firstObject.someProperty = 'I am a property on the first object';
console.log(firstObject.someProperty); // prints out "I am a property on the first object"
console.log(secondObject.someProperty); // prints out "undefined", because that instance of Object doesn't have a "someProperty" property

In JavaScript, all constructor functions can have properties set on them. An important one is called prototype. Prototype is an object, a mapping of keys to values, which basically says, "Anything made with this constructor function will have the same properties as what's on the prototype." All other properties are referred to as "static" properties. Static properties are not passed onto instances created with the constructor function, but are instead properties you reference from the constructor function directly.

As an example of how prototype works, let's make a Dog constructor function:
Code:
var Dog = function() {};
Dog.prototype = {
  bark: function() {
    console.log('Woof!');
  }
};
Dog.isDog = function(animal) {
  return animal.bestFriend === 'Human';
};

var rover = new Dog();
rover.bestFriend = 'Human';

rover.bark(); // Prints out "Woof!"
rover.isDog; // undefined, would throw an error if you tried to call it
Dog.isDog(rover); // true

There's a lot to unpack here, but what we've done is the following:
1. I want to be able to create Dogs, so I will make a constructor function for Dog.
2. I want every Dog to be able to bark. When it does, I want it to say "Woof!"
3. I want to be able to tell if an animal is a Dog, but that's not something that I want a particular dog to tell me. Instead of an "instance" of a dog telling me if any animal is a dog, I will make it so that the Dog constructor function (or Dog class) is capable of determining whether any animal is a dog by passing it an animal and seeing if its bestFriend property is "Human".
4. OK! I'll make a Dog. I'll call it rover. I'll say that rover's bestFriend is "Human".
5. Now, I want rover to bark. So I tell him to bark() and he says "Woof!"
6. Now I want to know if rover is a dog, except I don't want rover to tell me if he's a dog. I want to show rover to the Dog constructor and have it tell me if he's a dog. So, I'll use the Dog.isDog functionality and pass in rover.
7. Rover's bestFriend is "Human", so Dog.isDog came back with true and told me that he is, in fact, a dog.

Now, you could've changed it so that "isDog" is on the prototype, too, where it just returns true for every Dog:
Code:
var Dog = function() {};
Dog.prototype = {
  bark: function() {
    console.log('Woof!');
  },
  isDog: function() {
    return true;
  }
};

var rover = new Dog();
rover.bark(); // Prints out "Woof!"
rover.isDog(); // true

Except, what if we make a Cat constructor, and at some point we want to know if an instance of a Cat is a dog?
Code:
var Cat = function() {};
Cat.prototype = {
  isDog: function() {
    return false;
  }
};

var garfield = new Cat();
garfield.isDog(); // false

What about for Birds? Or Frogs? Are we going to add an "isDog" function to every single one of those constructor functions? It's easier to just make it a "static" function on the Dog constructor, so that you can simply call "Dog.isDog(garfield);", "Dog.isDog(jeremiah);", etc.

In short, static properties are when you need a property that isn't particular to a single "instance" of a constructor function. If you need properties that are different from instance to instance, or you want something that only pertains to every instance of that constructor, you set them as "instance properties". In the case of Window_Base, _iconWidth and _iconHeight are static properties because they aren't specific to any instance of Window_Base, they're just information that Windows want to have for knowing where and how to draw icons.

Now, back to the prototype - you can "inherit" or "extend" from constructor functions by using their prototypes, then continuing to add more properties to it. Let's say we wanted to make a Corgi constructor. It's still a Dog, so we want it to have that bark function, but we don't want to rewrite the bark function again. We could say:
Code:
var Corgi = function() {};
Corgi.prototype = Object.create(Dog.prototype);
Corgi.prototype.wiggleButt = function() {
  console.log('I will wiggle my fluffy butt at everybody!');
};

var pancake = new Corgi();
pancake.bark(); // Prints out "Woof!"
pancake.wiggleButt(); // Prints out "I will wiggle my fluffy butt at everybody!"

rover.bark(); // Prints out "Woof!"
rover.wiggleButt; // undefined - only Corgis have the wiggleButt function and rover is a generic Dog

We've copied the Dog.prototype onto the Corgi.prototype, then added another function to the Corgi.prototype, so that Corgis do everything Dogs do, but can also wiggleButt.

You see that pattern all over the place in the RMMV code base, so while it can be a bit of a headscratcher when you're first starting out, it's how JavaScript before ES6 handled "inheritance". Nowadays, JavaScript actually has classes and the "extends" keyword, though under the hood, it ultimately just creates constructor functions. With Windows in RMMV, for example, Window_Base is used to mean, "Every Window, regardless of what it does, has these Base prototype properties and functions." Then you might see, "Window_Selectable" extends Window_Base, but also needs to add some functions to move a cursor around (so that you can select options). However, not every Window needs to select options, sometimes a Window just needs to show information, so "Window_Help" also extends Window_Base, but it doesn't want the functionality that "Window_Selectable" does. They both copy Window_Base's prototype, but then move on and set a bunch of their own properties.

As to your question about primitives - there's a constructor function for String, one for Number, and one for Boolean. So while you can have primitive strings, numbers, and booleans, you can also create "new String()", "new Number()", and "new Boolean()". These constructors have their own prototype methods - String.prototype.split, or Number.prototype.toFixed, for example.

The String, Number, Boolean, Array, etc. constructors all "inherit" from the Object constructor, so everything the Object prototype has, so too do Strings, Numbers, Booleans, Arrays, Functions, etc. You can see it in the console:

VQ6BHbW.png
It is not a contradiction. Javascript Objects are like data lists, dictionaries in Python or structs in C like languages. But essentially even non-Objects in js are object in programming sense, because every variable in js can have properties/attributes and member functions.
I don't understand this but it's probably due to my lack of experience in programming in general.

Wow great extensive message Zevia, well done!

Some conclusions after reading (feel free to tell me if I'm off track):
1. JavaScript uses mostly objects which are essentially functions.
2.
While they don't have to be capitalized, common practice is to do so in order to indicate that they are constructor functions.
You mean Pascal casing I surmise.
3. A constructor function is the equivalent of a class in other programming languages.
4.
The most basic constructor function in JavaScript is a function called Object. Almost every time you generate a value in JavaScript, you're getting an Object of some sort.
Although there is more one than one type of constructor functions, the most basic constructor is called Object (there are others however).
5.
A constructor function is called that because you construct "instances" of something using that function. For example, if you want two Objects, you could say:
This one, I'm not so sure about instances. I'll ponder about it and get back to you about it.
For #1, it's the other way around - in JavaScript, Functions are also Objects. That's a quirk of JavaScript that isn't true in most other languages.

Otherwise, yes, your understanding is correct on other counts. Understanding an "instance" of a class (or a constructor function) can take awhile and tends to make more sense through sheer experience. It's like learning a game by reading the rules vs. playing a round or two.
In JavaScript, all constructor functions can have properties set on them. An important one is called prototype.
I thought a property was a placeholder for values when a function is called. For example:
upload_2019-8-17_22-18-36.png

My understanding was those were called properties in the JavaScript syntax.
(Whoops, yes; I forgot JS treats everything as objects, including primitives. 😔)

What you've highlighted there are the function arguments. These behave much like variables: they're defined and assigned values just before the rest of the function's code starts and they get deleted after it ends. They're members of the arguments object; arguments are properties of a function's arguments object, rather than of the function itself. 🙏

I think I mentioned before, but the terminology doesn't matter too much if you can see how everything works. 🙂
What you've highlighted there are the function arguments. These behave much like variables: they're defined and assigned values just before the rest of the function's code starts and they get deleted after it ends.
While I understand how that works, I now need to read again about properties! I thought properties became arguments when values were assigned to them.
When you define a function, the variables you set in the parentheses are called parameters, not properties. You pass arguments to a function and those arguments are assigned to the parameters.

Code:
function demo(param1, param2) {
  // param1 and param2 are parameters
  console.log(param1, param2);
}

demo('argument 1', 'argument 2'); // The strings "argument 1" and "argument 2" are arguments being passed to the demo function

In many languages, you can't run code if you try to call a function without an argument for every parameter. In JavaScript, if you don't pass an argument for a parameter, the value of that parameter defaults to undefined.
Ok so the content of (brackets) following a function are called parameters, gotcha.

Arguments are what takes the place of parameters which are essentially placeholders more or less, when you call a function for example.

In JavaScript, all constructor functions can have properties set on them. An important one is called prototype. Prototype is an object, a mapping of keys to values, which basically says, "Anything made with this constructor function will have the same properties as what's on the prototype." All other properties are referred to as "static" properties. Static properties are not passed onto instances created with the constructor function, but are instead properties you reference from the constructor function directly.

So in that case .prototype is a property.

Original thread: https://forums.rpgmakerweb.com/threads/complete-beginner-exploring-rpg_windows-js.112247/