RPG Maker Forums

Hello!


I am having some trouble trying to understand javascript's constructors and prototypes.


For example, here there is some code extracted from the engine:


// Scene_Map
//
// The scene class of the map screen.

function Scene_Map() {
this.initialize.apply(this, arguments);
}

Scene_Map.prototype = Object.create(Scene_Base.prototype);
Scene_Map.prototype.constructor = Scene_Map;

Scene_Map.prototype.initialize = function() {
Scene_Base.prototype.initialize.call(this);
this._waitCount = 0;
this._encounterEffectDuration = 0;
this._mapLoaded = false;
this._touchCount = 0;
};


I'll try to explain what I currently understand: first it creates the object Scene_Map. Then, inside the constructor (I'm calling constructor to anything that is inside the brackets in "function Scene_Map()", please correct me if I'm wrong) the intialize function later declared is called by this.initialize.apply(this, arguments). Then, the prototype of the object Scene_Base is passed onto the protoype of the object Scene_Map, thus inheriting its properties.


(I am aware what I've said could be utterly wrong, but describing how I read this code might help you to understand what I don't)


I always thought the prototype of a object was the same as the constructor, but apparently, I'm wrong.


For instance, I have no idea what is happening here: 


Scene_Map.prototype = Object.create(Scene_Base.prototype);
Scene_Map.prototype.constructor = Scene_Map;


And also, why you can't put everything that there is on Scene_Map.prototype.initialize inside the constructor of Scene_Map? Like this:


function Scene_Map() {
function initialize() {
Scene_Base.prototype.initialize.call(this);
this._waitCount = 0;
this._encounterEffectDuration = 0;
this._mapLoaded = false;
this._touchCount = 0;
};




As you can see, there is a lot I don't know. I would be really thankful if someone could help me to understand how prototypes and constructors work, what they truly are, and in what they differ.


Thanks! :)

Latest Threads

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,035
Messages
1,018,454
Members
137,821
Latest member
Capterson
Top