Javascript Object Create with Function Constructor Pattern

Description

Javascript Object Create with Function Constructor Pattern

function Person(firstName, lastName) {
    this.firstName = firstName;//ww  w . j  a  va  2s.  co  m
    this.lastName = lastName;

    this.getFullName = function() {
        return this.firstName + " " + this.lastName;
    };

    this.greet = function(person) {
        console.log("Hello, " + person.getFullName());
    };
}

let person1 = new Person("CSS", "HTML");
let person2 = new Person("John", "Doe");

person1.greet(person2);

function Person( firstname, lastname, age ) {
    if (firstname)
        this.firstname = firstname;//from   w ww .j av  a2  s.co m
    if (lastname)
        this.lastname = lastname;
    if (age)
        this.age = age;
}

let dave = new Person("Dave", "Smith", 28);

console.log( dave.firstname );    // "Dave"



PreviousNext

Related