2

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?

asked Jan 22, 2013 at 3:25
3
  • 3
    Why are you returning this.o from the constructor? I would just drop that, and your code should work. Commented Jan 22, 2013 at 3:27
  • Btw, how many game objects are you expecting to use? Commented Jan 22, 2013 at 3:29
  • It's a variable amount, considering I want to allow users to create their own in-game objects. Commented Jan 22, 2013 at 3:32

3 Answers 3

5

You should not return anything from this constructor.

Remove this:

return this.o;

Demo here.

If you return a value from a constructor, the object created will of the type of the returned value.

Demo here. If you see this demo, d.a returns 4 means new gameObject returned the this.o value instead of this which is the gameObject().

If you want to use prototype:

function gameObject() {
 this.o = {};
}
gameObject.prototype = {
 setimage:function(i) {
 this.o.img = i;
 },
 setDimensions:function(w, h) {
 this.o.width = w;
 this.o.height = h;
 },
 setPosition:function(x, y) {
 this.o.x = x;
 this.o.y = y;
 },
 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);
 },
 setClass:function(c) {
 this.o.cname = c;
 }
}

Demo here.

answered Jan 22, 2013 at 3:28
Sign up to request clarification or add additional context in comments.

1 Comment

That should not be such a generic statement; in this case nothing should be returned from the constructor :)
1

In javascript, the best way to create instance methods is using a prototype. This code should work:

function gameObject(){
 this.o={};
};
gameObject.prototype = {
 setimage: function(i){
 this.o.img=i;
 },
 setDimensions: function(w,h){
 this.o.width=w;
 this.o.height=h;
 },
 setPosition: function(x,y){
 this.o.x=x;
 this.o.y=y;
 },
 create: function(){
 var el=document.createElement("div");
 el.className="object "+this.o.cname;
 el.style.width=width*this.o.w;
 e.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);
 },
 setClass: function(c){
 this.o.cname=c;
 }
};

The issue with how you were doing it before was returning something - you don't need to do that.

Ja͢ck
174k39 gold badges269 silver badges317 bronze badges
answered Jan 22, 2013 at 3:31

Comments

0

Here's an example of using function methods within functions. I find this pattern easier to read and understand vs using classes or prototypes.

function Foo() {
 // Using this. makes the barf function accessible from Foo instances.
 // can also be written as: function this.barf (item) = {
 this.barf = (item) => { 
 console.log(`ate a ${item} and barfed!`);
 };
 // the eat function is internal to Food only (not using this.)
 let eat = (food) => {
 console.log(`ate: ${food}`);
 };
 // We can access eat within the function, but not outside.
 eat("apple"); 
 // We can also execute arbitrary code within the function (unlike using just an object to keep track of functions).
 console.log("too much foo'd"); 
}
let fooTest = new Foo();
fooTest.barf("pear");

Read more https://stackoverflow.com/a/7295712/1931185

answered Feb 11, 2023 at 19:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.