7 kyu

Factory Functions #1 - Creating profile for people

583 of 592donaldsebleung

Description:

Factory Functions #1 - Creating profile for people

Overview

In Javascript, there are basically two types of constructors - those in classes and factory functions/constructors. A factory function (otherwise known as a factory constructor) is a function that returns an object literal. This means that lots of similar objects can be easily created and instantiated without ever using the new keyword. According to this article, there are a number of benefits of using factory functions over using class syntax:

  1. Instantiating and object from a class requires the new keyword to be used; in contrast, with factory functions, the new keyword is not required at all
  2. If a class constructor is called without the new keyword (e.g. Animal() instead of new Animal()), the global namespace will be polluted. No such danger exists when using factory functions
  3. One of the conventions when using class syntax is that the name of the class is always in UpperCamelCase regardless of the programming language you are using. By using factory functions in Javascript instead, the programmer no longer feels the obligation to follow the awkward conventions of class syntax (i.e. the programmer will feel free to use lowerCamelCase in Javascript to name his/her factory constructor)
  4. Factory functions are much more flexible tha class constructors in general
  5. Factory functions (and other Javascript OOP methods that do not include classes and classical inheritance) allow inheritance of an object from multiple ancestors while a class can only inherit from one superclass

There's also the argument that by overusing classical inheritance and creating multiple layers of inheritance (for example, class Dachshund extends Dog, where class Dog extends Animal), if a bug occurs, it has to be fixed everywhere because for example after you fixed the superclass you will probably also have to fix the subclass as the behaviour of super may have already changed after the bug fix.

However, that article aside, I do not agree with all of his viewpoints, at least not at this stage, because I personally find class syntax much more readable and easy to understand and at this stage (of creating only very small programs with Javascript) I have not yet run into any of the problems he has mentioned yet.

However, who knows which one is better (classes or factory functions) if he/she hasn't tried both implementations? Therefore, in this Kata, you will be asked to create a basic factory function.

Task

Define a factory function person (yes, all lowercase letters) which accepts the following parameters in the following order:

  1. firstName
  2. lastName
  3. age
  4. gender
  5. employed
  6. occupation
  7. married

The names of the object's keys should be identical to the names of the parameters mentioned above.

The object should (then) also have the following methods:

  1. sayName - this should return the full name of the person (e.g. {firstName: "John", lastName: "Doe"} => "John Doe" // when the sayName() method is called)
  2. introduce - this should return a string of the following format: "Hello, my name is FULL_NAME. I am AGE years old. I am a GENDER."

IMPORTANT: For the introduce() method of the person, each sentence is separated from the next by exactly TWO whitespace characters.

In the next Kata of this series, we will be investigating how "inheritance" / object composition is done through factory functions.

Articles of Interest

The articles below may help you complete this Kata:

  1. Factory Functions Tutorial
  2. The Two Pillars of Javascript Part I - Why constructors and classes in Javascript are a mess (HIGHLY RECOMMENDED READ)
Fundamentals
Tutorials
Object-oriented Programming

Stats:

CreatedMar 31, 2016
PublishedApr 1, 2016
Warriors Trained1050
Total Skips14
Total Code Submissions2088
Total Times Completed592
JavaScript Completions583
CoffeeScript Completions7
Total Stars25
% of votes with a positive feedback rating84% of 125
Total "Very Satisfied" Votes99
Total "Somewhat Satisfied" Votes13
Total "Not Satisfied" Votes13
Ad
Contributors
  • donaldsebleung Avatar
  • smile67 Avatar
  • user8436785 Avatar
Ad