Advanced JavaScript 101: Prototypes — Adding functions to Object Constructors within IIFEs

//start IIFE
(function () {
//Person Constructor
function Person(name, age) {
this.name = name;
this.age = age;
}
//Sample people
var personOne = new Person('Luke', 24);
var personOne = new Person('Leia', 24);
//Prototype added to Person Constructor
Person.prototype.calculateBirthYear = function(currentYear){
//do something
console.log(currentYear - this.age);
}
//Call function
personOne.calculateBirthYear(2018); //logs to the console: 1994
//close IIFE
})();
IIFE, Constructor, and Prototype printing to the console calculateBirthYear in vanilla JavaScript

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Neidley

Andrew Neidley

More from Medium

How to build a Roman Numeral to Integer Function in JavaScript

Converting UTC DateTime to Local DateTime (MSSQL + JS)

A brief history to brief history of Java Script

How To Fix Object.Map Is Not A Function Error In JavaScript