JSFiddle - React, Tailwind, and code Playground

by waichan

JavaScript

//Article
class Article {
    constructor(builder) {
        this.title = builder.title;
        this.category = builder.category;
        this.isPublic = builder.isPublic
        this.isFree = builder.isFree;
        this.price = builder.price || 0;
    }
    render(){
        console.log(this)
    }
}

// builder
class ArticleBuilder {
    constructor(title, category){
        this.title = title;
        this.category = category
        this.isPublic = false
        this.isFree = true
    }
    makePublic(){
        this.isPublic = true;
        return this;
    }
    makePaid(price){
        this.isFree = false;
        this.price = price;
        return this;
    }
    build(){
        return new Article(this)
    }
}

const firstArticle = new ArticleBuilder('First Article', 'News').makePaid(100).makePublic().build();
const secondArticle = new ArticleBuilder('Second Article', 'Music').build()
firstArticle.render()
secondArticle.render()