markberning
12/15/2017 - 2:00 AM

JavaScript Object Constructors

/*
A CONSTRUCTOR 
is just a template for an object that you can then use over and over again. You don't have to create an brand new object from scratch with all the properites and methods. 
*/

var currentDate = new Date();
/*creates an new Date object*/
/*we can do the same with our own created objects*/
var course02 = new Course();

/*how to create an object constructor*/
function Course(title,instructor,level,views){
  this.title = title;
  this.instructor = instructor;
  this.level = level;
  this.views = views;
  this.updateViews = function(){
    return ++this.views;
  }
}

/*then use it to create your new objects*/
var course01 = new Course('JavaScript Essential Training','Mor10',1,4);
var course02 = new Course('ES6','Eve Porcello',1,0);

/*to create an array of courses*/
var courses = [
    new Course('JavaScript Essential Training','Mor10',1,9),
    new Course('ES6','Eve Porcello',1,0)
  ]

/*to call specific items from the array..*/
console.log(courses[0].instructor); // Mor10
console.log(courses[1].title); // ES6
console.log(courses[0].updateViews()); // 5