Redfern Dev

Factory Function To Create An Object In JavaScript

09 April, 2016

Use a constructor function that returns an object. You can then create multiple people by passing in the first and last name arguments the createPerson function.

var createPerson = function(firstName, lastName) {
    return {
        firstName: firstName,
        lastName: lastName,
        sayHi: function() {
        return "Hi there";
    }
};
var johnDoe = createPerson("John", "Doe");
var janeDoe = createPerson("Jane", "Doe");