DEV Community

Discussion on: The Mediator Pattern in JavaScript

abhinav1217 profile image
Abhinav Kulshreshtha • Edited on

Can you elaborate as if I am five?

This isn't how we create public and private code inside though,
 we do it by what's being returned using closure. 
We returned an object which is exposed to the caller.
remco profile image

I was about to write an elaborate explanation, but Douglas Crockford already wrote an extensive article about this 😄:

The keyword is "closure", an example is visible in a constructor-function:

function Person(firstName, lastName) {
  function fullName() {
    return `${firstName} ${lastName}`;

  this.introduce = function() {

When a Person is created we can call introduce on it, but not fullName, essentially making fullName private.

Keep in mind, closures are not limited to constructors! A closure is defined as "a function with its variable scope". Simplified that means that a function 'remembers' the scope it was defined in.

I hope it helps!

For more info you can check