all groups > flash actionscript > january 2005 >
You're in the

flash actionscript

group:

constructing classes



constructing classes dnk NO[at]SPAM canada.com
1/5/2005 9:02:11 PM
flash actionscript: Hi there, I am reading a book on mx 2004, an I am just starting to learn
actionscript, and I have been following the code examples in the book. I am
just in a section about constructing classes. Now the code snippet I am posting
below is just for refernce, and not meant to be working in anyway. Now the part
I am wondering about is the 'this' portion of the code. In the 'properties' of
the class. What is the 'this' for? is it just for refernce so that flash knows
they are properties of an object?

//create the object class constructor
Car = function(color:String, speedLimit:Number, startSpeed:Number){
//instantiate a few properties
this.color = color;
this.speedLimit = speedLimit;
this.speed = startSpeed;
Re: constructing classes NSurveyor
1/5/2005 9:46:32 PM
"this" is used to target the current object. In this case, the car. So, when
declaring variables, the color property will not just be defined, but it will
be one of the properties of the car. For example, if you did:

Car = function(acolor:String, speedLimit:Number, startSpeed:Number){
//instantiate a few properties
ccolor = acolor;
speedLimit = speedLimit;
speed = startSpeed;
}
//Now, if you want to see a property of car, if you try:
someCar = new Car('0x000000',10,3);
trace(someCar.ccolor);//undefined is outputed;
trace(ccolor);//0x000000 is outputed;

But, if you write "this" like the code shows, then the properties will belong
to the car:
Car = function(acolor:String, speedLimit:Number, startSpeed:Number){
//instantiate a few properties
ccolor = acolor;
speedLimit = speedLimit;
speed = startSpeed;
}
//Now, if you want to see a property of car, if you try:
someCar = new Car('0x000000',10,3);
trace(someCar.ccolor);//0x000000 is outputed
trace(ccolor);//undefinedis outputed;

BTW, I changed all 'color' 's, because Flash already has a color class, which
can screw things up.
AddThis Social Bookmark Button