There are many ways to call functions in JavaScript, but this just isn't working for me. Could someone please tell me exactly what I'm doing wrong?
I tried prototyping (e.g. gameObject.prototype = {};
), but that didn't work for some reason. Now I'm just trying to assign the methods directly within the function, and that isn't even working.
What's wrong with this picture?
function gameObject() {
this.o = {};
this.setimage = function(i) {
this.o.img = i;
};
this.setDimensions = function(w, h) {
this.o.width = w;
this.o.height = h;
};
this.setPosition = function(x, y) {
this.o.x=x;
this.o.y=y;
};
this.create = function() {
var el = document.createElement("div");
el.className = "object " + this.o.cname;
el.style.width = width * this.o.w;
el.style.height = height * this.o.h;
el.style.position = "absolute";
el.style.top = height * this.o.y;
el.style.left = width * this.o.x;
map.appendChild(el);
};
this.setClass = function(c) {
this.o.cname = c;
};
return this.o;
}
What I want is something like this:
var d = new gameObject();
d.setClass("class");
d.setDimensions(0.8, 0.15);
I'm still fairly new to object oriented programming, so I don't even know if my vocabulary is correct. What is it that I'm trying to do and what's the proper way to do it exactly?