What Are Methods?

By: Dusty Arlia
Published on September 21, 2012
Last Updated on Friday, July 17, 2015 at 11:51 PM
Total Updates: 8

Methods exist in object-oriented programming. The actions an object can perform are called methods. All methods are functions, but they differ from functions in that they are associated with a specific object. Certain methods will not work on certain objects.

Methods are easy to recognize because they have parentheses immediately following their name. An example of an object would be an array and some methods of an array would be pop(), push(), shift() and unshift(). Here is an example of a method:

document.write('Hello World');  

This example above shows the document object using its write() method to print the string Hello World to the screen. The write() method is attached to the document object. The dot in between the object and its method connects the two. This is called dot-syntax, and is used to access an object's properties or methods.

You can create your own method in an object literal or you can attach it later, after the object has been declared, using dot-syntax. Adding a method to an object is very similar to adding a property value.

Just like functions, methods can be passed input in the form of arguments and can output values using return statements.