Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save 0xgdr/180813647de6e5af82ab4b3ea8dfc277 to your computer and use it in GitHub Desktop.

Select an option

Save 0xgdr/180813647de6e5af82ab4b3ea8dfc277 to your computer and use it in GitHub Desktop.
Factory Function To Create An Object In JavaScript :: 2016-04-09

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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment