Eloquent JavaScript by Haverbeke Marijn
Author:Haverbeke, Marijn [Marijn Haverbeke]
Language: eng
Format: epub, mobi
Tags: COMPUTERS / Programming Languages / JavaScript
ISBN: 9781593272937
Publisher: No Starch Press
Published: 2011-02-10T16:00:00+00:00
Animating Life
We will need bind (or method) when implementing the step method of a terrarium. This method has to go over all the bugs on the grid, ask them for an action, and execute the given action. You might be tempted to use each on the grid and just handle the bugs we come across. But if you do this, when a bug moves south or east, we will come across it again in the same turn and allow it to move again.
Instead, we first gather all the bugs into an array and then process them. This method gathers bugs, or other things that have an act method, and stores them in objects that also contain their current position:
Terrarium.prototype.listActingCreatures = function() { var found = []; this.grid.each(function(point, value) { if (value != undefined && value.act) found.push({object: value, point: point}); }); return found; };
When asking a bug to act, we must pass it an object with information about its current surroundings. This object will use the direction names we saw earlier ("n", "ne", and so on) as property names. Each property holds a string of one character, as returned by characterFromElement, indicating what the bug can see in that direction.
For this, we’ll write a method listSurroundings and add it to the Terrarium prototype. It takes one argument, the point at which the bug is currently standing, and returns an object with information about the surroundings of that point. When the point is at the edge of the grid, # is shown for the directions that would bring the bug outside of the grid, so the bug will not try to move there.
To go over all the possible directions, we’ll just use the each method of the directions dictionary that we defined before. This will give us the “direction point” objects for those directions (things like Point(0, 1)), which we can add to the center we were passed in order to get the coordinates we are interested in.
Terrarium.prototype.listSurroundings = function(center) { var result = {}; var grid = this.grid; directions.each(function(name, direction) { var place = center.add(direction); if (grid.isInside(place)) result[name] = characterFromElement(grid.valueAt(place)); else result[name] = "#"; }); return result; };
The two methods defined previously are not part of the external interface of a Terrarium object; they are internal details. Some languages provide ways to explicitly declare certain methods and properties “private” and signal an error when you try to use them from outside the object. JavaScript does not, so you will have to rely on comments to describe the interface to an object. Sometimes it can be useful to use some kind of naming scheme to distinguish between external and internal properties, for example by prefixing all internal ones with an underscore (_). This will make accidental uses of properties that are not part of an object’s interface easier to spot.
Next is one more internal helper method, the one that will ask a bug for an action and carry it out. It takes a creature and the point at which the creature is sitting as arguments.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12569)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7061)
Microservices with Go by Alexander Shuiskov(6824)
Practical Design Patterns for Java Developers by Miroslav Wengner(6741)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6681)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6087)
The Art of Crafting User Stories by The Art of Crafting User Stories(5615)
NetSuite for Consultants - Second Edition by Peter Ries(5554)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5353)
Kotlin in Action by Dmitry Jemerov(5062)
