faqts : Computers : Programming : Languages : JavaScript : Language Core : Functions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 10 people (30%) answered Yes
Recently 2 of 9 people (22%) answered Yes

Entry

How can two functions (created as properties of a customs object) share information between them ?

Aug 11th, 2004 08:00
Eric Greenblatt, Bemi Faison, Kenneth Joergensen,


Functions that are properties of custom objects, are referred to as 
methods. Both can receive arguments and perform many operations. The 
only difference is that methods have a parent object, full of data and 
other methods, that it can use in it's routine. The parent-object is 
referenced by the "this" statement...
Let's begin with the basics. You call (or invoke) methods, as you would 
functions, except you must use the dot-syntax to target the method; 
targeting tells JavaScript where the function exists. For example, an 
object definition called "Car" has a method "Wash()". If you create an 
instance of the Car object, named "myCar", the statement needed to 
invoke the Wash() method is "myCar.Wash()".
This is how you would invoke any other method of the Car object, 
because you know the name of the instance. But how could one method 
invoke another method, without knowing the name of the instance?
Methods can refer to any other method within the same function, using 
the "this" statement. In a method, "this" refers to the instance of the 
parent object. Thus, if the Car object also had a method called "Drive
()", the Wash() method could call the Drive() method by 
invoking "this.Drive()". Use the "this" statement inside a method to 
reference any property of the intance as well. So, if the Car object 
has a "speed" property, you could reference the value of the speed 
property in the Drive() method, uding "this.speed".
Within a method instance names are irrelevant because they can always 
vary. The "this" keyword simply references the parent object (in this 
case Car), which gives any method access to all the other methods and 
properties of the parent object.