JSFiddle - React, Tailwind, and code Playground

by Darby Rathbone

JavaScript

var cat = (function() {
  var name = "bob";

  var secret = function() {
    console.log(name, "doesn't have any secrets");
  };

  return {
    getName: function() {
      return name;
    },
    miaow: function() {
      // we can call te private method anywhere we want
      secret();
      console.log("miaow");
    }
  }
})();

console.log("the cat's name is", cat.getName());
// but this will cause an error,
// because the method isn't visible from the outside
cat.miaow();
cat.secret();