Using Object.create

// Create an object variable with person prototype
const personPrototypes = {
    greeting: function(){

        // Template literal with backtiks
        return `Hello there ${this.firstName} ${this.lastName}`;
    },
    getsMarried: function(newLastName){
        this.lastName = newLastName;
    }
}

// Create an object called mary - Object.create will take in prototype
const mary = Object.create(personPrototypes);

// Add properties to this mary object
mary.firstName = 'Mary';
mary.lastName = 'Williams';
mary.age = 30;

mary.getsMarried('Thomson');

console.log(mary.greeting());

const gabor = Object.create(personPrototypes, {
    firstName: {value: 'Gabor'},
    lastName: {value: 'Flamich'},
    age: {value: 36}
});

console.log(gabor);

console.log(gabor.greeting());
Was this page helpful?

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *