# Protecting my Javascript

## Well, what do we have here?

Good OO practice is to encapsulate your data. I recently wrote a script that managed some data and methods for a mobile application to write WiFi information to an NFC. My first design looked something like this:

Except this is terrible: SSID and pass are global variables and exposed to the whole world. Additionally, the network formatting is fragile and difficult to expand. I decided to upgrade to the following and store everything in an object.

## Object Time

Much better. There’s only a single global variable now. The formatNetwork function checks all of the non-variable properties of the wifiWriter object and adds them to the object, so we can add more fields to the string without breaking everything. I did end up adding a field, so this paid off sooner rather than later.

It’s still not encapsulated. Anyone can access the two variables and modify them directly. My OO training tells me that this is horrible, and I don’t trust myself not to screw things up somehow. We’ll encapsulate it in a function for best results, returning an object that has methods to access and modify the variables.

## It’s Function Time

PROBLEM. My clever way of looping through the variables no longer works, since its a function and not an object anymore. It might be possible to pull the variables out in a similar manner, but you know, what if I wanted to add additional fields that weren’t to be incorporated? Let’s wrap it an object!

This satisfies my need for good code writing.