Module pattern

				
					
let Study = function (course, level) {
  this.course = course;
  this.level = level;
};

Study.prototype.courseLevel = function () {
  console.log(this.course + " is for " + this.level);
};

Study.prototype.enquiry = function () {
  console.log("I just want to have enquiry. I am yet to think of enrolling...");
};

let study1 = new Study("Javascript", "Beginners");
let study2 = new Study("Advanced Javascript", "Professionals");
let study3 = new Study("ES new concepts", "Proficients");
let study4 = new Study("Typescript", "Beginners");

study1.courseLevel();
study2.courseLevel();
study3.courseLevel();
study4.enquiry();

//Output
// Javascript is for Beginners
// Advanced Javascript is for Professionals
// ES new concepts is for Proficient

				
			
				
					class service {
  getCourses = (id) => {
    return [
      {
        course: "Javascript",
        level: "Beginners",
      },
      {
        course: "Typescript",
        level: "Advanced",
      },
      {
        course: "React",
        level: "Advanced",
      },
    ];
  };

  save = (data) => {
    console.log(data + "--saving to DB");
  };
}
export default service;

				
			
				
					//*..Module Pattern.. */


import service from "./service.js";
var dbService = new service();

class Study {
  constructor(data) {
    this.course = data.course;
    this.level = data.level;
  }
  courseLevel = function () {
    console.log(this.course + " is for " + this.level);
  };

  save = function () {
    dbService.save(this.course + " is for " + this.level);
  };
}

let study1 = new Study(dbService.getCourses()[0]);
console.log(study1);
study1.courseLevel();
study1.save();

let study2 = new Study(dbService.getCourses()[1]);
console.log(study2);
study2.courseLevel();
study2.save();

//Output
// Javascript is for Beginners
// Javascript is for Beginners--saving to DB
// Typescript is for Advanced
// Typescript is for Advanced--saving to DB

				
			

Leave a Comment