What Are Methods?
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
unshift(). Here is an example of a method:
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.