bethrobson / Head-First-JavaScript-Programming

418 stars 344 forks source link

Chapter 13 Robot game solution (p. 583 in pdf book) #20

Open tygamchenry opened 7 years ago

tygamchenry commented 7 years ago

Hello,

Why is a function needed under the unlock function? The following doesn't work... why doesn't it?

Game.prototype.unlock = function() { if (this.level === 42) { console.log(this.name + " is blasting you with laser beams."); } }

...

robby.unlock(); rosie.unlock();

bethrobson commented 7 years ago

because we don't want lasers only at level 42; we want lasers for all robots that reach level 42 so they keep them after that too. By adding the function to the prototype, you can call deployLaser after you've reached level 42 and gone beyond.

tygamchenry commented 7 years ago

Ok, I can wrap my head around that. Thank you for the prompt responses (again)!

Tyga

On Jul 20, 2017 1:29 PM, "Elisabeth Robson" notifications@github.com wrote:

because we don't want lasers only at level 42; we want lasers for all robots that reach level 42 so they keep them after that too. By adding the function to the prototype, you can call deployLaser after you've reached level 42 and gone beyond.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bethrobson/Head-First-JavaScript-Programming/issues/20#issuecomment-316790656, or mute the thread https://github.com/notifications/unsubscribe-auth/ARggdhr6d162sFvixbIpx3_aLQKsK_X0ks5sP5yAgaJpZM4OeWQd .