Programming on a Jerker PART 7: The End

Warning: this is a post entirely for those Jerkers, I mean “Jerks”, who program JavaScript at their Ikea desks.

The last step is to wrap up the constructor functions in an object literal, called “fauna”, as below. This serves to hide the privitizeNewVariables() via the Immediately-Invoked-Function-Expression or IIFE. This is a copy of the code from step 2.

console.clear();
var fauna = (function (){
  privitizeNewVariables=function (specs) {
    if (!specs.is_private) {
      var members = Object.assign({}, specs);
      members.is_private = true;
      return members;
    }
    return specs;
  },
  newAnimal=function (specs) {
    var members = privitizeNewVariables(specs);
    members.inheritance_type_list = ['Animal'];
    whenInDanger = function () {
      try{
        console.log('When in danger ', members.common_name);
        members.movesBy();
      }catch (e){
        console.log('Error - whenInDanger() has no movesBy()');
      }
    };
    var isA = function(object_type){
      if (members.inheritance_type_list.indexOf(object_type)>-1) {
        console.log(members.common_name, 'is a', object_type);
      }else{
        console.log(members.common_name, 'is not a', object_type);
      }
    }
    return Object.freeze({
      whenInDanger: whenInDanger,
      isA: isA
    });
  },
  newSnake=function (specs){
    var members = privitizeNewVariables(specs);
    members.movesBy = function () {
      console.log('Moves By: slithering');
    };
    colorScheme = function () {
      console.log('Color scheme :', members.color_scheme);
    };
    aPrivateFunction = function (){
      console.log('I only exist inside a Snake object');
    };
    var an_animal = newAnimal(members);
    members.inheritance_type_list.unshift('Snake');
    return Object.freeze({
      whenInDanger: an_animal.whenInDanger,
      isA: an_animal.isA,
      movesBy: members.movesBy,
      colorScheme: colorScheme
    });
  };
  return {
    newAnimal:newAnimal,
    newSnake: newSnake
  }
})();
var animal_specs = {common_name: 'Alf the animal'};
var an_animal = fauna.newAnimal(animal_specs);
animal_specs.common_name = "does not change Alf's common_name";
an_animal.whenInDanger();
console.log(an_animal);
console.log('-');
var snake_specs = {common_name: 'Snorky the snake',
 color_scheme:'yellow'};
var a_snake = fauna.newSnake(snake_specs);
a_snake.whenInDanger();
console.log('-');
a_snake.colorScheme();
a_snake.isA('Animal');
a_snake.isA('Snake');
a_snake.isA('Bear');
console.log('-');
console.log(fauna);

Here is the console output for the above:

When in danger Alf the animal
Error - whenInDanger() has no movesBy()
Object { whenInDanger=function(), isA=function()}
-
When in danger Snorky the snake
Moves By: slithering
-
Color scheme : yellow
Snorky the snake is a Animal
Snorky the snake is a Snake
Snorky the snake is not a Bear
-
Object { newAnimal=function(), newSnake=function()}

Fini

I rest my case. I cannot find a single reason for not using Douglas Crockford’s Classless JavaScript.

Prototypal JavaScript seems slightly more compact, but is much more error-prone, harder to understand, and more difficult to change than classless JavaScript.

Part  1  2  3  4  5  6  7