javascript - Classless object in shorthand notation -


so, dived head first in marijn haverbeke's eloquent javascript , swam directly electronic life chapter.

there observe spontaneous generation of object :

 {"#": wall,"o": bouncingcritter} 

wall , bouncingcritter defined elsewere :

function wall() {}     function bouncingcritter() {   this.direction = randomelement(directionnames);   this.name = word(); };  bouncingcritter.prototype.act = function(view) {   if (view.look(this.direction) != " ")     this.direction = view.find(" ") || "s";   return {type: "move", direction: this.direction}; };  bouncingcritter.prototype.tostring=function(){     return this.name +"\t"+ this.direction; } 

---> don't sorcery happening there. well, see we're creating object on fly, , since it's going used once, no need define class. ok. understand hitherto anonymous object has 2 properties, classes, instantiated several times down road. i'm starting flutter i'll along. 1 of (wall) quite empty, later prove useful "nothing" better 'undefined'. fine.

what don't use of string constants "#" , "o" on left hand side of colon. please expand on ?

heartly thanking,

what don't use of string constants "#" , "o" on left hand side of colon. please expand on ?

the property has called something. rather unhelpful names tell nothing property holds.

… until @ context. link provided:

the “#” characters in plan represent walls , rocks, , “o” characters represent critters.

it's using # name wall property because symbol used render in ui. ditto "o"/citter.

(that still isn't great name though: tightly couples internal name presentation of it, start fall apart if ui later changed use (for example) pretty graphics instead of ascii art)


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -