On March 2, 2004 08:20 pm, Shawn wrote:
> "type" is used in Javascript, so consider it a reserved word.  I know you
> can override some reserved words, but that's asking for trouble.

I knew someone was gonna notice that :)

>
> Also, you're treeinfo function is slightly wrong.  It is a member function
> of a tree, but makes reference to an object it doesn't know about
> (my_forest).
> Replace "my_forest" inside the treeinfo function with "this".  But even
> then this will have problems.  The Tree class doesn't define a "type"
> property.

Yeah, which I know how to fix now thanks to you. Im still boggled as to why it 
could see my_forest was an object but couldn't see its member vars. Oh well, 
its trivial, but interesting. 

>
> I would rework your classes a little to put the TreeInfo function as a
> member function on the Forest class, and pass in the Tree instance you want
> the information from.  Of course, there are other ways to do this, and I
> realize this is only sample code - your real code may not be doing this....

Applying the same concept should work in my actual code. Guess I was thinking 
so hard my brain quit.

>
> As for your questions, yes the this.trees[i] in your MakeTrees function
> does point to your forest object.  This is because the MakeTrees function
> is a member function (or method) of the Forest class.  If you call
> MakeTrees directly (i.e. x = MakeTrees(), not x=my_forest.MakeTrees() ),
> then "this" refers to the function itself (if I remember right), and you'll
> have problems.

makes sense.

>
> So, your forest class can have tree objects in it.  The tree class and it's
> methods can also make reference to "this", but at that point, "this" is
> refering to the tree object.
>
> Oh, one last thing.  In your Forest function, the line "trees = new
> Array();" should read "this.trees = new Array();".  If you don't make this
> change, the trees variable will loose scope when the function is done.

a minor oversight in my 'type this example as fast as I can' mindset. 

>
> Not really a well laid out response, but I hope it points you in the right
> direction.  I'm "very" familiar with JS classes, so feel free to email me
> off list if you need any other tips/help.


Thanks a lot Shawn, this should be sufficient to get me going again. Hopefully 
I won't need to contact you off-list, but thanks for the offer.


>
> Shawn
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Nick W
> Sent: Tuesday, March 02, 2004 4:14 PM
> To: CLUG General
> Subject: [clug-talk] OT - Javascript frustration
>
>
> Sorry for the OT post but this makes no sense and every source I read tells
> me
> this should work.
>
> I have a stupid problem with a script Im writing. I have an object which
> contains an array of objects, which contain image objects. I set the
> onmouseover property of the images to run a function which needs data from
> one of the outermost object's properties. The said function can see the
> object, but not its properties. OK thats clear as mud, let me try a
> simplified example...
>
> <begin lame example>
>
> function Forest()
> {
> this.type = 'rainforest';
> trees = new Array();
>
> this.MakeTrees = MakeTrees;
> }
>
> function MakeTrees()
> {
> for(i = 0; i < 1000; i++)//make 1000 trees
>       {
>       this.trees[i] = new Tree();
>       }
> }
>
> function Tree()
> {
> this.img = new Image();
> //pretend I set the this.img properties here...
> this.img.onmouseover = TreeInfo;
> }
>
> function TreeInfo()
> {
> alert(typeof(my_forest));
> alert('This tree is in a ' + my_forest.type);
> }
>
> my_forest = new Forest();
> my_forest.MakeTrees();
> <end lame example>
>
> When you mouseover a tree, the first alert in TreeInfo() will tell you
> my_forest is an object, but the 2nd one will spit out "This tree is in a
> undefined". Why??
>
> Note, the above code was written very quickly and is probably unuseable,
> but it serves its purpose as a lame example.
>
> Its obviously some sort of scope issue with Java I dont get. AFAIK,
> my_forest
> should be global in this case. My other question is in MakeTrees() does the
> 'this' in 'this.trees[i]' point to my_forest? Of course the whole thing
> could
> be FUBAR....
>
> tks.
> --
> Nick W (nickw77 at shaw.ca)
> Registered Linux User #324288 (http://counter.li.org)
> MSN Messenger: [EMAIL PROTECTED]
> Yahoo: foolish_gambit
> ICQ: 303276221
> It's not our fault, we're outnumbered by stupid people ten to one.
>
> _______________________________________________
> clug-talk mailing list
> [EMAIL PROTECTED]
> http://clug.ca/mailman/listinfo/clug-talk_clug.ca
>
>
> _______________________________________________
> clug-talk mailing list
> [EMAIL PROTECTED]
> http://clug.ca/mailman/listinfo/clug-talk_clug.ca

-- 
Nick W (nickw77 at shaw.ca)
Registered Linux User #324288 (http://counter.li.org)
MSN Messenger: [EMAIL PROTECTED]
Yahoo: foolish_gambit
ICQ: 303276221
It's not our fault, we're outnumbered by stupid people ten to one.

_______________________________________________
clug-talk mailing list
[EMAIL PROTECTED]
http://clug.ca/mailman/listinfo/clug-talk_clug.ca

Reply via email to