1. Computing & Technology

JavaScript Objects

The Difference Between Methods and Properties

From , former About.com Guide

The two basic types of variables that can be contained within an object variable are called methods and properties. These actually correspond to types of variables that we have already seen and it is only because they are given different names when you reference them within an object that you don't already know exactly what they are. A method is code that can be run. It defines one of the ways in which a given object can behave. A method is basically a function that is attached to and interacts directly with a specific object or type of object.

Properties are all of the other types of variables that JavaScript supports with the only difference being that their scope is defined in terms of the object that they belong to rather than being local or global. Properties define the current state of the object and running a particular method belonging to the object is normally done in order to update the state of one or more of the properties of the object.

While not strictly accurate, you wont go too far wrong if you think of the properties and methods of an object as being functions and variables whose scope is contained within that object but which can be referenced from outside of that scope using the period notation after the name of the object itself.

©2012 About.com. All rights reserved.

A part of The New York Times Company.