2

Creational Design Patterns In JavaScript: A Brief Tutorial

 3 years ago
source link: https://hackernoon.com/creational-design-patterns-in-javascript-a-brief-tutorial-35w3304
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Constructor Pattern

In the constructor pattern, instead of returning the instance from the function, we use the new operator along with the function name.

0 reactions
function createFruit(name) {
    this.name = name; 
    this.showName = function () {
        console.log("I'm " + this.name); 
    }
}
const fruitOne = new createFruit('Apple'); 
const fruitTwo = new createFruit('Orange'); 
fruitOne.showName(); // I'm Apple
fruitTwo.showName(); // I'm orage

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK