1

Im trying to code pacman at the moment, and a came across a problem: Since all the Ghosts use the same pathfinding and are generally pretty simular, I want to use a prototype for them. The only property in which they really differ, is the way, they choose their target position. I'd like to give a function to the prototype and use it as a getter. Is that possible?

function Ghost(color,x,y,getterFunction){
    this.color = color;
    this.x = x;
    this.y = y;
    this.direction = "up";
    this.move = function(){
        //Pathfind towards this.target
    }
    this.target = getterFunction; //or something like this...
}

Thanks for your help :^)

Anton Ballmaier
  • 876
  • 9
  • 25
  • No. If that is where they differ, that is the thing you do *not* want to put on the prototype. – Bergi Jan 30 '17 at 15:24
  • 1
    Just use `this.target()` instead of `this.target` and your code should work. – Bergi Jan 30 '17 at 15:25
  • Have you considered using an ES6 class? You can use Babel to transpile the code to work in all browsers – royalsampler Jan 30 '17 at 15:26
  • @royalsampler I wouldn't know how to do it with classes anyways... But thanks to the Babel tip... I didn't know that. – Anton Ballmaier Jan 30 '17 at 15:28
  • @Bergi I thought of simply leaving it out of the prototype, but that makes it impossible to initialize the objects in one go. I need to add the functions afterwards... But the tip to simply not use a getter is great :) Tahnks! – Anton Ballmaier Jan 30 '17 at 15:31
  • I don't understand what the prototype has to do with the initialisation? Yes, leave it out of the prototype and add it in the constructor (or even later). – Bergi Jan 30 '17 at 15:53
  • Btw, whether it's a getter or not is totally unrelated from whether it sits on the instance of on the prototype, you [can define getters on both](http://stackoverflow.com/a/25998583/1048572). – Bergi Jan 30 '17 at 16:03

1 Answers1

4

@Bergi is right. You don't want to use it as a getter. If you tried to add it to the prototype it would be overwritten by every new ghost you create, since the prototype is a shared object.

The prototype is for shared functionality. Instance functionality belongs in the instance, i.e. In your constructor function.

Your move function should be on the prototype. But target should be an instance method. You could set a default method for target on the prototype. Any instance method would be called before looking to the prototype.

Example

function Ghost(color, x, y, target){
    // everything in here is instance specific
    this.color = color;
    this.x = x;
    this.y = y;
    this.direction = "up";

    if (typeof target === 'function') {
      this.target = target;
    }
}

// this is the prototype
Ghost.prototype.move = function() {
    //Pathfind towards this.target
    this.target()
}

Ghost.prototype.target = function() {
  // default target to use if no target provided at creation
}

So now, when you do this:

var redGhost = new Ghost('red', 0, 0, function() {
  //do something unique
})

You'll have a ghost that's red and has a custom target function. But if you do this:

var yellowGhost = new Ghost('yellow', 0, 0)

You'll have a ghost that uses the default target function you added to the prototype.

shadymoses
  • 3,273
  • 1
  • 19
  • 21
  • 1
    This is a good answer, but could really benefit from some sample code. – Jordan Running Jan 30 '17 at 15:36
  • Now this answer is *awesome*. A+++ would upvote again. – Jordan Running Jan 30 '17 at 15:46
  • Thanks :) You help really did help! – Anton Ballmaier Jan 30 '17 at 18:23
  • I have one question though: Is there a difference in adding the move function specificially to the prototype, or can I just leave it where it was? – Anton Ballmaier Jan 30 '17 at 18:25
  • If you leave it in the constructor, every instance will get a new copy of that function. If you want it to be shared via the prototype, you have to add it to the prototype. The prototype is a separate object. Each instance gets a reference to the prototype, so if you try to find a property on your instance that doesn't exist (such as move), it looks up the prototype chain to see if it can be found. – shadymoses Jan 30 '17 at 18:46