In the development world, not all code is created equally. For all you who are familiar, I’m not referring to people writing shitty code. I’m talking about the visibility level in our methods/functions throughout the code base, or app.
Before going any further, you first need to understand what visibility in coding actually means. When you hear the word visibility instantly in your comes the idea of being able to visually see something. Well, in programming that is essentially what we’re referring to. The difference is that we’re talking about visibility in a sense of the level of exposure a piece of your code base, or app, has to other areas.
That was a lot to take in right? Well let’s just take things back a bit, and take some time to look at some past articles that will help ease into this new knowledge.
- Code Breakdown: Object Oriented JS
- Code Breakdown: Object Oriented JS Pt. 2
- Code Breakdown: Object Oriented JS Pt. 3
- Development 1o1: OOJS Inheritance
To keep things aligned with the previous articles mentioned above, we’ll be discussing privilege levels with JS this time. Don’t worry though, later on in other articles we’ll be doing this in PHP and Rails as well!
The Three Privilege Levels of Development
Public
To get started we’ll begin with the privilege level everyone is familiar with even if they don’t know, Public. When setting a method or variable to public, you’re saying that it should be able to be accessible and editable from anywhere in the code base.
This privilege level is the most familiar one is because it’s the default for when a privilege level is not specified.
Protected/Privileged
Now this is where things start to become more strict. In most languages, protected is where a method can only be used by the class/constructor it is being declared in and any class/constructor that inherits from it.
So if you had two classes Person and Place, and Person had a protected method called isAlive. isAlive can be accessed by those that inherit from Person, and rewrite its functionality for its own needs. However, Place has no accesses to that method.
In JS more specifically, the equivalent of protected is known as privileged. For a method to be privileged means it can access private variables and methods without exposing them to the rest of the code.
Private
When you set something to Private, this is something like how most fathers would like to see their daughters. Accessibility to these are reliant to those in the immediate family or relatives and can’t be changed from the outside. The difference with code is that the immediate family is a class and the relatives are code sections that are directly related to that class, like inheritance. In OOP, the family is the model and the relatives are the controllers.
In JS more specifically, private is used to define variables and methods that are defined directly inside the constructor function. Private variables can be accessed by private and privileged methods, and private methods can be accessed by privileged methods.
Code Examples
Now that we’ve gained an understanding for what the different privilege levels are, let’s move into some real uses for them. I now want to reiterate that we’ll be going over this examples in JS, which does this differently than other languages for OOP. Now, let’s get to coding!
Public Variables and Methods
Setting public variables are as easy as declaring a necessary parameter for your constructor class. In fact, that’s exactly what it is.
function Person(name, age){
this.name = name, this.age = age;
}
The name and age variables being set by the entering parameters here are being created as public variables for created objects. These are also better known as instance variables. Keep the name instance variable in mind okay, it’ll come in handy later on.
Moving on, let’s take a look at public methods. Now if you’ve gone over the previous 101 series article on OOJS inheritance, this is something you already know how to do. I’ll even give you a small hint, to create public methods we use prototype. Got it right? Either way, take a look below.
Person.prototype = {
isAlive: function(){
return this.name + ' is alive';
},
older: function(years){
this.age += years;
return this.name + ' is now ' + this.age + ' years old!';
}
}
As you see above we are using our dear friend prototype to declare our public methods. Not hard to get at all right?!?
Private Variables and Methods
On a more strict note, let’s now move into the private sector of our code. Anyone who dare enter here without authorization will be beheaded, or have there favorite IDE discontinued. Which ever is more affective.
Private variables aren’t like their counter part public variables, they are hard defined inside the constructor class. Despite them being very strict and privacy conscious, you already know how to set them up inside your constructor. This being like any old JS variable starting with var.
function Person(name, age){
this.name = name, this.age = age;
var birthCity = 'London';
}
See what we did there with birthCity, simple right?
Private methods are also something your familiar doing, its just like defining a function inside of another function.
function Person(name, age){
this.name = name, this.age = age;
var birthCity = 'London';
function funny(){
return (this.name == 'Jamal') ? 'Cool name!' : 'Weird name!';
}
}
Privileged Methods
After going over the public and private privilege levels, this last one should be a breeze. So much so, let’s head straight to the example.
function Person(name, age){
this.name = name, this.age = age;
var birthCity = 'London';
function funny(){
return (this.name == 'Jamal') ? 'Cool name!' : 'Weird name!';
}
this.from = function(){
return this.name + ' is from ' + birthCity;
}
}
Simple as that, and we’re all done!