12.4. MethodsMethods are functions that are associated with objects and are typically used to perform a task or access an object's data. We invoke methods with the function call operator, ( ), using the dot operator to separate the method name from its object: objectName.methodName( ) For example: ball.getArea( ); // Call the getArea( ) method of ball As with properties, methods are defined for a class and then invoked on individual object instances. However, before we see how to create methods within a class, we're going to bend the rules of good object-oriented programming and attach a method to an individual object instance of the built-in Object class. Once we understand how a single method works on an isolated object, we'll apply the concept correctly to a class of our own. A method is essentially just a function stored in an object property. By assigning a function to an object property, we turn that function into a method of the object:
Once a function resides in an object property, we may invoke that function as a method, like this: myObject.sayHello( ); // Displays: "hello world" Truth be known, you can also assign a function to an array element (instead of to an object property) and invoke that function using the call operator, like this: var myList = new Array( ); myList[0] = greet; myList[0]( ); // Displays: "hello world" But when a function is invoked as a method of an object, something very special happens to the function -- it gains the ability to retrieve or set the properties of the object to which it is attached. Let's see how this works with our sayHello property. First, we add a msg property to our generic object (again, we're bending the OOP rules for the sake of the demonstration . . . it's bad form to attach a custom property to an individual object instance): myObject.msg = "Nice day, isn't it?"; Now we adjust greet( ) so that it displays the value of msg:
Notice the keyword this at the end of line 2? That's our connection to myObject. When executed as a method of myObject, sayHello( ) is effectively passed an invisible argument called this containing a reference to myObject. In the body of the function, we use this to access myObject's msg property as this.msg. When we invoke myObject.sayHello( ), the expression this.msg resolves to myObject.msg, which becomes "Nice day, isn't it?". A method can both retrieve and set the value of its host object's properties. Example 12-1 demonstrates a method that retrieves the value of two properties of an object, performs a calculation based on those values, and then sets the value of a third, new property. (Once again, because our current focus is methods, the object we're using is only an object instance. In the next section we'll learn how to add object methods properly with a class.) Example 12-1. Implementing a Method that Sets a Property
It's more typical to have a method return a value instead of setting a property. Let's revise our rectangle's setArea( ) method to do just that, as shown in Example 12-2. Example 12-2. Returning a Value from a Method
The this keyword can be a tricky concept because it involves some sleight of hand by the interpreter. To illustrate how the preceding code works, consider Example 12-3, which exposes the implied relationship of this to its object and method. (Example 12-3 is fictitious because this is a reserved keyword and would cause an error if used as shown.) Example 12-3. A Fictitious Example Using this Explicitly
Methods may also be assigned to object properties as function literals. By using a function literal to create a method, we avoid the work of first creating our function and then assigning it to a property. Example 12-4 reworks our rectangle object from Example 12-2 showing the use of a function literal. Example 12-4. Implementing Methods with Function Literals
Methods give us enormous control over objects. When used with built-in objects, methods are one of our primary tools for controlling the Flash movie environment. In this section we've seen both how to store a function in a property (thereby making it a method) and also how to invoke a function through a property. These two endeavors have very similar syntax, but very different results. It's crucial to understand the difference.
Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|