Book Image

Learning Underscore.js

By : Alexandru Vasile Pop
Book Image

Learning Underscore.js

By: Alexandru Vasile Pop

Overview of this book

Table of Contents (14 chapters)
Learning Underscore.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Migrating to a functional programming style


We will revisit the examples in the folder oop-underscore from the source code for this chapter and adapt them to use standalone functions instead of object methods:

  • We will keep the classes as simple data structures and we will even preserve the inheritance

  • We will move all the functionality around validation and any object methods outside the original classes

Because we want to preserve the existing functionality, we will leave the test specifications unchanged together with the code that displays the example output. First of all, we will move the validation logic into a separate validations.js file. The object constructor functions for Contact and Client are validating the argument's length and we will move this functionality into a new function:

validateArgsLength: function(argsLength, argsArray) {
  if (argsArray.length != argsLength) {
    throw {
      name: "ArgumentsException",
      message: "The arguments length is incorrect."
    };
  ...