JavaScript: refering to the default object when using with()

  • Mirkoo
  • Born
  • Born
  • No Avatar
  • Joined: Jun 05, 2004
  • Posts: 2
  • Status: Offline

Post June 5th, 2004, 1:18 pm

Hello,

can anybody tell me how I can refer to the default object in the with-statement? Is there an expression or statement or something?

I have figured out a workaround, but it's not really elegant. I will post it to point out my problem more clearer:

<pre>
for (n=0; n < document.links.length; n++)
with(document.links[n]) {
alert('Default object in with-Statement is: '+ childNodes[0].parentNode); // this shows the original object (in fact, the URL)
...
}
</pre>

Of course this is not "the right way", but it works I guess (though I'm not sure, if every link has childNodes - but then, I want a general solution, working for any object).

I hope you understand the problem, since I'm not a native-english speaker ;)

Mirkoo
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 5th, 2004, 1:18 pm

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post June 5th, 2004, 2:03 pm

I have no idea about a with statement, but try

Code: [ Select ]
this


however, I would tend to just use variables:
Code: [ Select ]
for (n=0; n < document.links.length; n++){
  var currentLink = document.links[n];
  alert(currentLink.href);

}
  1. for (n=0; n < document.links.length; n++){
  2.   var currentLink = document.links[n];
  3.   alert(currentLink.href);
  4. }


I have used with in Visual BASIC and always found it to be very clumsy. The fact that javascript does not enforce strict typing makes it really easy to just use variables for this type of work.

Plus you can have multiple object variables. Using multiple with's just gets messy.

Hope this helps :D
CSS website design tutorials
  • Mirkoo
  • Born
  • Born
  • No Avatar
  • Joined: Jun 05, 2004
  • Posts: 2
  • Status: Offline

Post June 5th, 2004, 3:03 pm

I have tried "this" already - doesn't work.

But using variables is a good idea, thanks. I've already done so in other cases, don't know why I didn't do so here. Seems very handy and useful.

Mirkoo

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: ScottG and 216 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.