Create custom methods in a JavaScript object

Create methods for an object

Reference:

You can create methods for an object in JavaScript by utilizing the following code:

First, you must have an object to add a method to, the yellow is an example of a method.
You can reference this on page 442 of the Head First JavaScript Book

function Blog(body, date){
   this.body = body;
   this.date = date;
  
   //Return a string representation for the blog entry
   this.toString = function(){
      return "["  + (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" +
         this.date.getFullYear() + "] " + this.body;
  } 
}


 

 
Maint