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: