0

I'm attempting to refactor my code to use anonymous closures as I want to make some of the methods private. I've never done this before and nothing I try seems to work. My tests are failing saying: "player.bowl is not a function in file"

var Player = function (name) {
  var username = name;

  this.getName = function () {
    return username;
  };

  var bowl = (function (frame) {
    var hitPins, knockDownPins;
    return {
      knockDownPins: function () {
        hitPins = Math.floor(Math.random()*(frame.pins+1));
        frame.pinsStanding(hitPins);
        frame.bowlsHadThisFrame += 1;
        return hitPins;
      }  
    };
  })();
}

What have I done wrong? Also are there any good online resources where I can find out more about anonymous functions? :)

Emily
  • 29
  • 5
  • You seem to be missing a parenthesis somewhere, although that first one, you don't need it. Also, how are you calling `Player`? – elclanrs Feb 01 '15 at 21:30
  • Oh yes! Although I only put that in when typing my code into stack overflow. It's not in the original failing code. – Emily Feb 01 '15 at 21:31
  • player = new Player("Emily") – Emily Feb 01 '15 at 21:32
  • 1
    Why would a `player` instance have a `bowl` function? You just declared a private variable that gets destroyed right after the constructor finishes to run. – plalx Feb 01 '15 at 21:33
  • You need to make it a `this.bowl` property, instead of a `var`. And even then, the bowl won't be a function but that object `{knockDownPins:…}` that is returned from the IEFE. – Bergi Feb 01 '15 at 21:36
  • Hi plalx, I don't really follow. I originally had the bowl as a Player prototype method, but moved it into my constructor when deciding to make it private. Is this part of the issue? Apologies if I'm being dim - I've only been learning javascript for the last fortnight. – Emily Feb 01 '15 at 21:36

0 Answers0