your comments do clear it up a bit tho thanksâŚ
*Originally posted by Jubba *
**your comments do clear it up a bit thothanks⌠**
YAY
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]
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
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
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
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: