Closures can provide encapsulation when Creating Objects in Javascript
Objects are funny things in Javascript. If you’re coming from a language like Ruby with classical inheritance you’ll probably be surprised that in Javascript’s prototype system there are no such things as classes. The patterns are different but we can still achieve what’s important about object oriented software namely objects that encapsulate data and behavior
.
Today I’m going to show you two different ways we can create a simple database object that supports the basic CRUD operations in Javascript.
Object Literal
The simplest way to create our database is to just declare it as a singleton object with some instance data and functions. We’ll use the “starts with underscore” naming convention to denote “private” data but that is just a convention and not enforced.
This is pretty simple and if you try it out you’ll see it actually works.
The biggest downside of this approach is that there is no encapsulation. We can get at the _data
instance variable directly to read or change it.
Encapsulation via a Closure
By wrapping our Database
in a closure we can encapsulate our private functions and data. The way to read this is that
- Database is a function that defines a closure.
- The variables within that closure have access to each other (i.e.
_create
has access to_data
). - When the
Database
is called it returns an object that explicitly exposes 4 functions and nothing else.
When we test it we see that it still works.
The _data
is encapsulated and not accessible from the outside
There are other patterns for working with objects in Javascript that take advantage of Javascript’s prototype system but I don’t have time to go into that today … perhaps in a future post.