Methods
Methods are functions that can be associated with an object and operate on an object's data

First, write a function that does what you want the method to do. Within the function, use the keyword this to refer to the object the function will become a method for. this allows you to access the objects properties.

Here is an example of how a method might be created to test if an answer is correct. First a function must be created. When it is called the keyword this will contain a reference to the object the method is associated with. In this example this.answer is used to get the value in the answer property of the object.

function correct(guess){    
 if (guess == this.answer)    
    return true   
  else        
return false
                         } 


Associate the function you write with a method name for an object. This allows you to call your function using the method name. You can assign the function to a method name inside a constructor function much as you can assign values to properties. There is another way to associate your function with an object method name. The alternative is to use the prototype object which is also demonstrated below.

Once the function has been defined a reference to the function must be assigned to the object when it is constructed. This can be done in either of two ways. One is to assign the function reference to the method name inside the constructor function. To get a reference to a function use the function's name without the round brackets. For example, to get a reference for the function named correct() use the word correct. Assigning a function reference to an object property allows you to use the name of the property as a method (if you tack on () brackets when you call the method).

function Question(n1, n2){  
  this.question = "What is the sum of " + n1 + " and " + n2 + "?"   
  this.answer   = n1 + n2     this.correct = correct  

}











original link