Share this Page URL

Chapter 12. Functions > Functions Built In to Flash - Pg. 262

Functions gObject.ageVerify = function():Number{ return(this.age); } trace(gObject.ageVerify()); //output:25 262 That was a lot of work for not a big payoff. We could have simply traced the property itself. Therefore, let's build on the previous example and combine multiple properties in an expression that we may want to use again and again. In this example, we'll create an object called myRec. Then we'll create a function that gets the area and the perimeter of the object. The idea is that no matter how often the properties we use change, the function will perform the same calculation on our object. Here's the code: //First, create the object var myRec:Object = new Object(); //Now add a property and two methods to our object myRec.w = 5; myRec.h = 10; myRec.getArea = getRecArea; myRec.getPerim = getRecPerim; //Here, create the functions function getRecArea():Number{ return this.w * this.h; } function getRecPerim():Number{ return (this.w * 2) + (this.h * 2); } //Finally, invoke the methods trace(myRec.getArea()); trace(myRec.getPerim()); //output: 50 // 30