MV Adding variable to multiple subobjects? (SOLVED)

Starting off by apologizing for not understanding the terminology in what determines an object. I call all things "object", in my mind functions, variables, arrays and such are a type of object and so it might be bit confusing... I don't get variables, arrays and functions confused though. I think that objects might be like a class in java, that seems inconclusive to me though, but I really haven't gotten used to all the differences yet. And I am also no expert in Java...

So hopefully if the word "object" seem misplaced below, just use context instead of terminology to understand what I am trying to say 🙂 Like trying to understand what a foreigner says by basically doing a little of the guesswork for them 🙂

I want to add a variable, or function or whatever, to one of my objects. All the objects in that object are supposed to have two variables of the same name.

I've added the story in the below comments of the example code.

JavaScript:
X ={};      //Creating the X object(Might be mistaken on proper terminology).

X.Y = {};     //Creating an object of X called Y.

X.Y.x = function(){}; //Creating functon x of X.Y.

X.Y.y = function(){}; //Creating functon y of X.Y.

X.Y.prototype.z = 0; //Wanting to create a variable z for both function x and y.

But then I just get this message:

Code:
Uncaught TypeError: Cannot set property 'z' of undefined
    at <anonymous>:1:17

So as I understood it the "prototype" is just a substitute for all of the objects within an object...

Best regards,
Bishiba



Edit:
Changed title from "Adding multiple variables to subobjects?" to "Adding variable to multiple subobjects?".
So you've hit several concepts on the head here (including the fact that everything in Javascript is an object, so well done on getting that) but you've made a few fundamental mistakes in your logic.

The first thing to understand is that the x = {} construct is creating an object literal, but you can't use prototype with it because it doesn't have a constructor. That's why your code doesn't work: you've set X.Y to an object literal but haven't defined a constructor for that object.

You can verify this by trying X.Y.y.prototype.z = 0; which will work. Why? Because you defined X.Y.y as a function, which creates the constructor for you as part of creating the function object.

You can, however, define a prototype for an object after creating it. So you could, for example, do

X.Y.prototype = Object.prototype

And could then do X.Y.prototype.z = 0;
Thanks a lot I found the below part and figured I can work it out from that code when going over the rpg_windows.js.
1625356873881.png

But your explanation really clarifies for me sort of what it actually means rather than just being able to do it. For example clarifying why I got it working another time, just like you said about functions.

JavaScript:
Game_Picture.prototype.imgPath = function(imgId) {
        imgName = new Image;
        return imgName.src = "img/pictures/" + $gameScreen._pictures[imgId]._name + ".png";
}
1625357112333.png

It essentially just added to my confusion! 😛

But I need to understand what literals are, I am currently logicing my way into believing it's more of a "set in stone" type of thing, where it is litterally an object rather than being built on another object. But I shall read about it shortly 🙂

Best regards and thanks again,
Bishiba

So you've hit several concepts on the head here (including the fact that everything in Javascript is an object, so well done on getting that) but you've made a few fundamental mistakes in your logic.

The first thing to understand is that the x = {} construct is creating an object literal, but you can't use prototype with it because it doesn't have a constructor. That's why your code doesn't work: you've set X.Y to an object literal but haven't defined a constructor for that object.

You can verify this by trying X.Y.y.prototype.z = 0; which will work. Why? Because you defined X.Y.y as a function, which creates the constructor for you as part of creating the function object.

You can, however, define a prototype for an object after creating it. So you could, for example, do

X.Y.prototype = Object.prototype

And could then do X.Y.prototype.z = 0;
@bishiba Instead of editing the threat title to say that it's solved, you can report your own post and just type "solved". Otherwise us mods won't see that your thread is solved. Since I caught this one bypure coincidence, I will will close it since it's solved.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/adding-variable-to-multiple-subobjects-solved.138082/