Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

Two Points to use the toString-method in classes

The toString-method is rarly used. But it is a good practice,
because it helps to understand youre code, example:
// Example 1(without toString)Car = function (pHexColor) { this.hexColor = pHexColor;}objAudi = new Car();trace(objAudi); // output: [object Object]// Example 2(with toString)Car = function (pHexColor) { this.hexColor = pHexColor;}Car.prototype.toString = function () { return '[object Car]';}objAudi = new Car();trace(objAudi); // output: [object Car] So the difference is the Information we get
while tracing an object.
But toString can also be interesting for
so called hashes(in AS also called value-objects)
but first we need to modify our class a little bit:
// Example 3Car = function (pHexColor) { // unique-id this.intId = ++arguments.callee.intGeneratedObjects; this.hexColor = pHexColor;}// class-property or static-propertyCar.intGeneratedObjects = 0;Car.prototype.strClassName = 'Car';Car.prototype.toString = function () { return '[object '+this.strClassName+this.intId+']';}objAudi = new Car();trace(objAudi); // output: [object Car1] Now lets see how easy it is to store our object in
hash-table: // create the listener-hashhashListeners = new Object();// store our object as listenerhashListeners[objAudi] = objAudi;// delete our object from the listener-hashdelete hashListeners[objAudi]; OK, that was not really a big deal,
regards, Holger...:-)

Posted by hOk at April 17, 2003 12:52 PM

Comments (0)


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place