I’m incredibly new to javascript and the way classes and methods work are confusing me.
Basically I have code like this:
function container(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.sumUp = function addUp(x, y, z) {
var a = x + y + z;
};
}
What I want to do is elsewhere in my code use the function defined within the container, using the values in container. How do I actually go about doing this?
Something along the lines of
container1 = new container (1, 2, 3);
container.sumUp(this.x, this.y, this.z);
Or something like that. I’m very confused and thinking I’m going about the whole thing wrong.
I think you want somwthing like this:
But I recomend:
That is how it works (short):
In JavaScript you have
objects, they are like hashes:And you can get or set values by keys:
In your case
Containeris function which will build your object. When you donew Container(1, 2, 3);it creates an empty object, and execute the function in the context of the object.