Javascript Object Create with factory method

Introduction

function createPerson(firstName, lastName) {
    return {//from   www  .  j  a  v a  2s.  c  om
        firstName: firstName,
        lastName: lastName,
        getFullName: function() {
            return this.firstName + " " + this.lastName
        },
        greet: function(person) {
            console.log("Hello, " + person.getFullName() +
                  ". I'm " + this.getFullName());
        }
    };
}

let johnDoe = createPerson("John", "Doe");
let janeDoe = createPerson("Jane", "Doe");

johnDoe.greet(janeDoe);



PreviousNext

Related