CHAPTER ELEVEN
JavaScript Is…
Science is what we understand well enough to explain to a computer. Art is everything else we do.
Donald Knuth
I understand there are people in this world who do not like JavaScript. I’ve gotten into enough late-night battles about CoffeeScript to have heard all the vitriol. I’ve been generally unmovable about this. I think curly braces are elegant, I think semicolons are enchanting, and I think duck typing is adorable. I thought writing this chapter would be a good opportunity to share my favorite things about JavaScript.
JavaScript Is Dynamic
JavaScript takes advantage of virtual machines for just-in-time (JIT) compilation (see V8, Node.js, and Spider Monkey for some examples). JavaScript makes excellent use of closures by having variables that exist both on the global level and the functional level.
Consider this function:
functionCheckForPrefix(name){varprefix="Dr.";if(name.indexOf(prefix)==-1)returnfunctionAddPrefix(){returnprefix+name;}}
JavaScript’s closures give us the ability to reference variables defined in the containing function. In this instance it enables us to keep the separation of concerns while not repeating ourselves at the same time.
JavaScript has an eval function that allows us to concatenate values and evaluate them at runtime. eval makes things slower, as it adds a compilation step, so it’s to be used sparingly; however, it does allow us to create macros, which are another dynamic language feature. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access