Skip to content

Instantly share code, notes, and snippets.

@MartinJHammer
Last active December 30, 2016 16:31
Show Gist options
  • Save MartinJHammer/8af3a99395439638fe352e6ed47aadb6 to your computer and use it in GitHub Desktop.
Save MartinJHammer/8af3a99395439638fe352e6ed47aadb6 to your computer and use it in GitHub Desktop.
Requires jquery
/*
* Module : personCreator
* Purpose : Ease the creation of Person objects, and keeps track of created people.
* Created by : Martin Jul Hammer
* Date : 24-12-2016
*/
(function (personCreator, $, undefined) {
"use strict";
var peopleCreated = [];
/**
* Prototype (class) representing a person.
* @private
* @param {String} firstName - The person's first name.
* @param {String} lastName - The person's last name.
*/
var Person = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + " " + this.lastName;
};
};
/**
* Creates a new person object, and saves it in peopleCreate.
* @public
* @param {String} firstName - The person's first name.
* @param {String} lastName - The person's last name.
* @returns {Person} The created person.
*/
personCreator.createPerson = function (firstName, lastName) {
var person = new Person(firstName, lastName);
peopleCreated.push(person);
return person;
};
/**
* Get all created people.
* @public
* @returns {[]} An array of person objects created.
*/
personCreator.getCreatedPeople = function () {
return peopleCreated;
};
}(window.personCreator = window.personCreator || {}, jQuery));
// Use module when the dom is ready.
$(function () {
var person = personCreator.createPerson("Martin", "Hammer");
personCreator.createPerson("Bob", "Fischer");
personCreator.createPerson("Alice", "Archer");
console.log(person.fullName()); // Logs Martin Hammer.
console.log(personCreator.peopleCreated); // Logs undefined.
console.log(personCreator.getCreatedPeople()); // Logs an array with 3 People objects.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment