Skip to Main Content
Essential ActionScript 3.0
book

Essential ActionScript 3.0

by Colin Moock
June 2007
Intermediate to advanced content levelIntermediate to advanced
948 pages
27h 2m
English
O'Reilly Media, Inc.
Content preview from Essential ActionScript 3.0

Chapter 3. Instance Methods Revisited

In Chapter 1, we learned how to create instance methods. In this chapter, we’ll expand that basic knowledge by studying the following additional instance-method topics:

  • Omitting the this keyword

  • Bound methods

  • State-retrieval and state-modification methods

  • Get and set methods

  • Extra arguments

Along the way, we’ll continue developing the virtual zoo program that we started in Chapter 1. But before we begin, take a minute to reacquaint yourself with the virtual zoo program. Example 1 shows the code as we last saw it.

Example 1. Zoo program
// VirtualPet class
package zoo {
  internal class VirtualPet {
    internal var petName;
    private var currentCalories = 1000;
    private var creationTime;

    public function VirtualPet (name) {
      this.creationTime = new Date();
      this.petName = name;
    }

    public function eat (numberOfCalories) {
      this.currentCalories += numberOfCalories;
    }

    public function getAge () {
      var currentTime = new Date();
      var age = currentTime.time - this.creationTime.time;
      return age;
    }
  }
}

// VirtualZoo class
package zoo {
  public class VirtualZoo {
    private var pet;

    public function VirtualZoo () {
      this.pet = new VirtualPet("Stan");
    }
  }
}

Omitting the this Keyword

In Chapter 1, we learned that the this keyword is used to refer to the current object within constructor methods and instance methods. For example, in the following code, the expression this.petName = name tells ActionScript to set the value of the instance variable petName on the object currently being created: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Essential ActionScript 2.0

Essential ActionScript 2.0

Colin Moock
The ActionScript 3.0 Quick Reference Guide

The ActionScript 3.0 Quick Reference Guide

Jen deHaan, David Stiller, Darren Richardson, Rich Shupe
ActionScript 3.0 Cookbook

ActionScript 3.0 Cookbook

Joey Lott, Darron Schall, Keith Peters

Publisher Resources

ISBN: 0596526946Errata