AS1 OOP: Object Basics
         by senocular  

Valueof and toString
There are some methods that are inherent to all objects including basic number, string, and boolean variable "objects". These are the valueOf and toString methods.

The valueOf method represents the base value of an object. This is the value specified by that object"s type. A number, for example, would have a number value associated with valueOf – the value of the number associated with that variable – this independent of the fact if the variable is a number object or a basic number variable value. When you perform mathematical calculations with an object, Flash uses the valueOf method of an object to know how to treat that object in the figuring of the results.
 

// number object
num = new Number(10);
trace(num.valueOf()); // traces 10

 
// basic number
num = 5;
trace(num.valueOf()); // traces 5

 
// math expressions use valueOf
trace(num * 10); // traces 50;
trace(num.valueOf() * 10); // traces 50;

The toString method represents the string interpretation of the value of the object. Often, you see no difference in this as for generally every instance of toString, you get the same value as valueOf, just in string form. Numbers would still be their number values, just not as numbers but as strings (i.e. "5" instead of 5). When ever you trace an object, it"s toString is used to get the string value of that object to display in the output window which is basically just a textfield window in Flash Note that for strings, valueOf will be exactly the same as valueOf since the value of a string is a string.

// number
num = 2;
trace(num); // traces 2
trace(num.toString()); // traces 2

 
// string
str = "two";
trace(str); // traces two
trace(str.toString()); // traces two

 
// strings toString equals valueOf
trace(num.valueOf() === num.toString()); // traces false (2 is not strictly "2")
trace(str.valueOf() === str.toString());// traces true

Now you may not ever have to deal with these methods, at least not with normal objects such as these (they may come in handy with custom objects). However, you can take advantage of these methods and re-write them to work in your favor (only be aware you can only rewrite these methods for object-defined variables and not basic numbers, strings or booleans).

As an example lets create some variable called age. This will represent your age. Its value will be 16. Congratulations, you are now 16. Being the clever 16 year old that you are, you devise all kinds of ways to make your school reports seem as long as possible without them really being all that long. This includes large fonts, exaggerated line spacing and the classic margin crunching. Also, any numbers you include in your reports you will specifically write out as opposed to just putting the number. The number 34 for example, would be thirty four. And of course 16 would be sixteen. So, if you ever have a report needing the number 16 (i.e. age) and you want not only its value 16, but also as it exists in text as sixteen," you can specify in actionscript, as for some reason you enjoy writing actionscript to complete school reports, a unique toString method to handle the age value as it should appear in strings.

age = new Number(16);
age.toString = function(){
return "sixteen";
};

 
trace("My current age is");
trace(age); // traces "sixteen"
trace("In 5 years I will be");
trace(age+5); // traces 21

Rewriting toValue of in this manner cal also be done. In fact, you can effectively change the value of an object without actually replacing or overwriting the object itself just by redefining these two methods.

Prev Page
 



SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.