Practicing Prototypes
Finally, let’s work on this
and objects linked via prototype (Chapter 4, Pillar 2).
Define a slot machine with three reels that can individually spin()
, and then display()
the current contents of all the reels.
The basic behavior of a single reel is defined in the reel
object below. But the slot machine needs individual reels—objects that delegate to reel
, and which each have a position
property.
A reel only knows how to display()
its current slot symbol, but a slot machine typically shows three symbols per reel: the current slot (position
), one slot above (position - 1
), and one slot below (position + 1
). So displaying the slot machine should end up displaying a 3 x 3 grid of slot symbols.
function randMax(max) {
return Math.trunc(1E9 * Math.random()) % max;
}
var reel = {
symbols: [
"♠", "♥", "♦", "♣", "☺", "★", "☾", "☀"
],
spin() {
if (this.position == null) {
this.position = randMax(
this.symbols.length - 1
);
}
this.position = (
this.position + 100 + randMax(100)
) % this.symbols.length;
},
display() {
if (this.position == null) {
this.position = randMax(
this.symbols.length - 1
);
}
return this.symbols[this.position];
}
};
var slotMachine = {
reels: [
// this slot machine needs 3 separate reels
// hint: Object.create(..)
],
spin() {
this.reels.forEach(function spinReel(reel){
reel.spin();
});
},
display() {
// TODO
}
};
slotMachine.spin();
slotMachine.display();
// ☾ | ☀ | ★
// ☀ | ♠ | ☾
// ♠ | ♥ | ☀
slotMachine.spin();
slotMachine.display();
// ♦ | ♠ | ♣
// ♣ | ♥ | ☺
// ☺ | ♦ | ★
Try to solve this yourself first.
Hints:
Use the
%
modulo operator for wrappingposition
as you access symbols circularly around a reel.Use
Object.create(..)
to create an object and prototype-link it to another object. Once linked, delegation allows the objects to sharethis
context during method invocation.Instead of modifying the reel object directly to show each of the three positions, you can use another temporary object (
Object.create(..)
again) with its ownposition
, to delegate from.
Once you have code that works, compare your solution(s) to the code in “Suggested Solutions” at the end of this appendix.