View Full Version : [OOP] tracing instances of Class
ahmed
July 4th, 2003, 01:05 AM
myClass = function(name) {
this.name=name
}
instance1 = new myClass("a")
instance2 = new myClass("b")
instance3 = new myClass("c")
trace(instance1 instanceof myClass)
trace("\n")
for (var i in _root) {
trace(i instanceof myClass)
}outputs
true
false
false
false
false
false
this's been bugging me, anyone know what's wrong? ;(
Jubba
July 4th, 2003, 01:12 AM
I realize this isn't what you want, but if I do this, then it outputs fine:
myClass = function (name) {
this.name = name;
};
instance1 = new myClass ("a");
instance2 = new myClass ("b");
instance3 = new myClass ("c");
trace (instance1 instanceof myClass);
trace ("\n");
for (i = 1; i <= 3; i++) {
ext = _root["instance" + i];
trace (ext instanceof myClass);
}
what does the
for(var i in _root)
do and where does it come from? i is never defined. does that matter? I dunno...
lostinbeta
July 4th, 2003, 01:16 AM
myClass = function (name) {
this.name = name;
};
instance1 = new myClass("a");
instance2 = new myClass("b");
instance3 = new myClass("c");
trace(instance1 instanceof myClass);
trace("\n");
for (var i in _root) {
trace(_root[i] instanceof myClass);
}
:beam:
Jubba
July 4th, 2003, 01:16 AM
oh, and I have you on my buddy list, but you're never online... are you avoiding me?
lostinbeta
July 4th, 2003, 01:17 AM
Jubba: It is called a for/in loop, they can be quite useful sometimes.
http://www.kirupa.com/developer/actionscript/tricks/forin.asp
Jubba
July 4th, 2003, 01:17 AM
that makes sense lost...
lostinbeta
July 4th, 2003, 01:18 AM
Originally posted by Jubba
oh, and I have you on my buddy list, but you're never online... are you avoiding me?
Who? Me? Or Ahmed?
ahmed
July 4th, 2003, 01:19 AM
:beam:
that's great, how could i miss that :P :love:
jubs-
that would work too, only thing is, their names arent gonna be uniform :)
Jubba
July 4th, 2003, 01:19 AM
ahmed :)
lostinbeta
July 4th, 2003, 01:20 AM
Hmm, weird, I start with a new document, use this code..
myClass = function (name) {
this.name = name;
};
instance1 = new myClass("a");
instance2 = new myClass("b");
instance3 = new myClass("c");
for (var i in _root) {
trace(_root[i] instanceof myClass);
trace(_root[i].name);
}
And I get 2 extra undefined objects :q: I don't get it.
ahmed
July 4th, 2003, 01:21 AM
if me, I changed sn's :p
mine now is synOrange :beam:
Jubba
July 4th, 2003, 01:21 AM
I know nothing about AS in Flash anymore.
and you didn't answer my question :P
ahmed
July 4th, 2003, 01:22 AM
Originally posted by lostinbeta
And I get 2 extra undefined objects :q: I don't get it.
for (var i in _root) {
trace(_root[i]);
} ;)
edit:
the second one is 'myClass', and then comes the three instances of it, totals up to 5 :)
lostinbeta
July 4th, 2003, 01:22 AM
Originally posted by Jubba
I know nothing about AS in Flash anymore.
You lie.
h88
July 4th, 2003, 01:23 AM
- myClass
- $version
An if statment would sort it out.
lostinbeta
July 4th, 2003, 01:23 AM
Originally posted by ahmed
for (var i in _root) {
trace(_root[i]);
} ;)
edit:
the second one is 'myClass', and then comes the three instances of it, totals up to 5 :)
OMFG I knew that too... it totally friggin skipped my mind...lol.
lostinbeta
July 4th, 2003, 01:24 AM
Originally posted by h88
- myClass
- $version
An if statment would sort it out.
Surely would...
myClass = function (name) {
this.name = name;
};
instance1 = new myClass("a");
instance2 = new myClass("b");
instance3 = new myClass("c");
for (var i in _root) {
if (_root[i] instanceof myClass) {
trace(_root[i].name);
}
}
Jubba
July 4th, 2003, 01:25 AM
confuzzled... :(
oh and Ahmed, mine is --------- :)
lostinbeta
July 4th, 2003, 01:27 AM
Originally posted by Jubba
confuzzled... :(
oh and Ahmed, mine is ------- :)
:(
//constructor function to make new object
myClass = function (name) {
this.name = name;
};
//make new object with name "a", etc, etc.
instance1 = new myClass("a");
instance2 = new myClass("b");
instance3 = new myClass("c");
//use for in loop to cycle through objects on the _root timeline
for (var i in _root) {
//if the object found is an instance of the myClass object
if (_root[i] instanceof myClass) {
//trace it's name variable
trace(_root[i].name);
}
}
Any help?
Jubba
July 4th, 2003, 01:28 AM
oh I get it, I just don't see how it would do me any good with my scripting. I need to brush up, but I never have any time :(
Jubba
July 4th, 2003, 01:28 AM
your comments do clear it up a bit tho ;) thanks...
lostinbeta
July 4th, 2003, 01:29 AM
Originally posted by Jubba
your comments do clear it up a bit tho ;) thanks...
YAY :)
senocular
July 4th, 2003, 11:06 AM
since this is about tracing Ill add you can define a trace output for your class instances by defining a toString method, ie.
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[i] instanceof myClass) {
trace(_root[i]);
}
}
/* output:
myClass Object named c
myClass Object named b
myClass Object named a
*/
:)
lostinbeta
July 4th, 2003, 11:09 AM
Nice :thumb:
h88
July 4th, 2003, 11:22 AM
So toString is invoked whenever we assign it as a method for our constructor class?
lostinbeta
July 4th, 2003, 11:24 AM
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.
senocular
July 4th, 2003, 11:48 AM
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].
senocular
July 4th, 2003, 12:21 PM
... 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
h88
July 4th, 2003, 12:27 PM
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?
senocular
July 4th, 2003, 12:34 PM
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
:)
h88
July 4th, 2003, 12:41 PM
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?
senocular
July 4th, 2003, 12:45 PM
string() was just to convert the valueOf retrun from a number to a string :)
senocular
July 4th, 2003, 12:50 PM
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.
ahmed
July 4th, 2003, 01:51 PM
those are nice Sen =) :thumb:
[m]
July 4th, 2003, 02:18 PM
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:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.