November 11th, 2017

Understanding "this" in JavaScript


this in JavaScript is part of the language that always confused me. The keyword doesn't work like its Java counterpart where it refers to the object instance of a method or constructor. Often in the past when using this I took shortcuts such as the popular var self = this statement so I wouldn't worry about the value of this in nested functions.

What makes this so confusing is that it's set at runtime instead of author time (compile time)1. In other words it doesn't follow the rules of lexical scope that I am comfortable with. this is incredibly daunting for new programmers since its value can be different when calling the same function on separate occasions.


November 10th, 2017

ES6 Modules Run with Babel


ECMAScript 6

+3 More

In my last discovery post, I went over the revealing module pattern in JavaScript. Now I am recreating the same API with ES6 modules. The API code will look familiar:

The only change to the API is the export keyword. export is a new keyword in ES6 that reveals the function lyrics to other JavaScript code. Any JavaScript file can be a module if it exports functions/variables. Only one module can exist per file, and each module can export multiple items. Since one file is one and only one module, the name of a module is the filename. Code to import a module uses the import keyword:


November 9th, 2017

Closure & Lexical Scope in JavaScript Modules



In JavaScript there are multiple module patterns for creating APIs and separating concerns in code (as of ES6 there is also official module syntax in the spec). In the following code I created an API using the revealing module pattern. The name 'revealing module pattern' comes from the return statement at the end of the module - it 'reveals' functions to outside code.

This module provides lyrics for Taylor Swift songs (because who doesn't enjoy some T-Swift!) The return statement is the public API for the module. All interior details, such as the lyric variable, are hidden. This pattern harnesses the power of closure in JavaScript!


November 8th, 2017

Scope & Hoisting in JavaScript


JavaScript has quickly become one of the languages I use the most (probably second behind Java). Many people use JavaScript along with one of its many frontend frameworks (JQuery, AngularJS, etc.) without really knowing how the core language operates. I don't want to be one of those people!

This is my first of many discovery posts on JavaScript. Let's look at one of the basic concepts of the language: how variables interact with scope. Scope describes the area of a program where a variable is accessible (e.g. a variable declared in a function is only accessible within that function). Scope is also the execution environment for each line of a program. It consists of the variables and functions a program line is aware of. In JavaScript, scope can get a bit tricky.


November 6th, 2017

Creating a Simple Geographical Map with Neo4j and Cypher


Graph Databases

+2 More

Lately I've read about graph databases and their place in the NoSQL data storage universe. The graph database I've worked with is Neo4j, which is fun and easy to get started with. I found the user interface very enjoyable for viewing graphs and executing queries. I highly recommend it if you need a graph database solution.

Graph databases largest draw is related data storage and the speed at which you can query related data points (or in graph terms, nodes/vertices). Relationships are first class citizens, which allows related data queries to be executed by traversing relationships themselves. This is contrasted with a typical relational database where you have to find relationships through foreign keys or combine two tables with a very slow SQL JOIN operation1. The same slow query in a RDBMS (Relational DataBase Management System) is extremely quick in a graph database.