Status
Not open for further replies.

bishiba

Adept
Veteran
Joined
Apr 6, 2016
Messages
150
Reaction score
23
First Language
Swedish
Primarily Uses
N/A
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?".
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,744
Reaction score
2,847
First Language
English
Primarily Uses
RMMZ
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

Adept
Veteran
Joined
Apr 6, 2016
Messages
150
Reaction score
23
First Language
Swedish
Primarily Uses
N/A
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! :p

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;
 

Attachments

  • 1625357096766.png
    1625357096766.png
    5 KB · Views: 1

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,941
Reaction score
4,853
First Language
EN, FR
Primarily Uses
RMMZ
@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.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

Working in the IT industry:
- "How long does it take?".
- "It takes 1 month".
- "What if we add more people to the project?".
- "It would take 2 months".
Doing RPG Maker News for 16th September 2021

Stream is live! I am currently doing pixel art and will be practicing shading! Feel free to drop by!
wow, Surface Tension is really one MF of an FPS level huh
So, I got let go from my current job because my skill set didn't align with the department's goals anymore. I have some hope, because I am currently in contact with a manager from a different department, and they are desperate for more employees. We'll see what happens... :kaoswt:

Forum statistics

Threads
115,120
Messages
1,087,371
Members
149,605
Latest member
Sedenho93
Top