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.
But then I just get this message:
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 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?".