[OOP] tracing instances of Class

your comments do clear it up a bit tho :wink: thanks…

*Originally posted by Jubba *
**your comments do clear it up a bit tho :wink: thanks… **

YAY :slight_smile:

since this is about tracing Ill add you can define a trace output for your class instances by defining a toString method, ie.

[AS]
myClass = function (name) {
this.name = name;
};
myClass.prototype.toString = function () {
return "myClass Object named "+ this.name;
};
instance1 = new myClass(“a”);
instance2 = new myClass(“b”);
instance3 = new myClass(“c”);
for (var i in _root) {
if (_root* instanceof myClass) {
trace(_root*);
}
}
/* output:
myClass Object named c
myClass Object named b
myClass Object named a
*/
[/AS]

:slight_smile:

Nice :thumb:

So toString is invoked whenever we assign it as a method for our constructor class?

Yes, it is kind of like if you use Button.prototype.useHandCursor = false , the hand cursor value will automatically be inherited by all button objects and none of them will use the hand cursor by default.

well its always invoked in a trace. If not defined, by default your class will call the Object.prototype.toString. This is what traces [object Object]. You can delete that and then Flash will default to [type Object].

… while Im at it, valueOf :wink:

myClass = function (value) {
	this.value = value;
};
myClass.prototype.valueOf = function () {
	return this.value * 10;
};
myClass.prototype.toString = function () {
	return string(this.valueOf());
};

a = new myClass(5);

trace(a + 5); // 55
trace(a * 2); // 100
trace(a > 100); // false
trace(a < 100); // true
trace(a); // 50

But you’re still invoking the toString method? Are you trying to point out that the class’s properties are getting overwritten by the valueOf method without actually assigning?

toString is only run in trace(a). The other traces are tracing the retun of the operation; that being a calculation or comparison. Without valueOf, an object (like a) has no value despite I named a property in that object ‘value’. Without the valueOf those traces return:

NaN
NaN
false
false

valueOf lets you define a value for that object when its involved in such calculations/comparisons

:slight_smile:

I get it now, thanks for pointing that out, one more thing, we defined toString() method in MyClass, however, you didn’t use Object’s toString method for MyClass’s to convert the number into a string, instead, you used the global string function. I did a test, and it worked out using the toString() instead, so are those different programming techniques, or we’re invoking Number’s class toString?

string() was just to convert the valueOf retrun from a number to a string :slight_smile:

well since we’re screwing around with toString methods, you might find yourself wanting to change the Number toString as well… in doing that you’d get that new Number toString and not a basic number to string conversion which string() gives you.

I, too, typically use string() for direct conversion as its similar to the use of number() for the same thing (only the other way around) - just seems a little more consistent that way I think.

those are nice Sen =) :thumb:

So that wolud mean that toString is automaticly invoked when flast wants to add a string and nuber together? I dod not now that.

I learned something new today! :thumb: