HighDots Forums  

Re: Understanding this Code

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Understanding this Code in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ManoDestra
 
Posts: n/a

Default Re: Understanding this Code - 07-23-2003 , 08:25 PM






I admit I haven't used regular expressions much myself, but I can help you
on some other things here.

Regarding your numbered points...

1. This creates a new array with nothing in it. I wouldn't do it this way
personally. I would do var x = new Array(); or to be more precise I would
probably call it arrQueryString or something like that.

2. I generally use the same notation as you do, but this is also
legitimate. It is basically a standard collection iteration. For each
ELEMENT in COLLECTION, do such and such... Get it?

3. The unescape is to remove any of the character codes from the query
string. If you pass a value in the query string, it has to alter certain
characters, such as a space which then becomes %20 which effectively means
Hex 20 which is 32 the Ascii code for a space. The unescape translates "%20"
back to a space again. Don't know what the RegExp is doing though.

Peter.

"Andrew V. Romero" <rrstudio2 (AT) icqmail (DOT) com> wrote

Quote:
I recently posted a question that was about obtaining the variables in
the URL. I received a few great answers and am using the code that was
provided to me, however, seeing as I am still pretty new to javascript,
I was wondering if someone could help explain some of the commands in
this function. In addition to getting things to work, I also like to
have a full understanding of what is going on. Here is the code:

var x={}; // global variable referencing empty object
function loadURLVariables()
{
var searchPairs = location.search.substring(1).split("&");
for (var i in searchPairs)
{
var pair = searchPairs[i].split("=");
x[pair[0]]=unescape(pair[1].replace(/\+/g," "));
/* if this is the result of a form action */
}
}

1) var x={} - I think this creates a new object that is empty.

2) for (var i in searchPairs) - I haven't seen this notation before,
in the past I have used the typical i=0; i < searchPairs.length; i++.
Is this just short hand notation?

3) x[pair[0]]=unescape(pair[1].replace(/\+/g," ")); - This is the
line that really throws me. So we are creating an array in which the
key is pair[0], which allows me to call the variables by x.foo or
x[foo]. I have no idea what the whole unescape and regular expression
is for.

Thanks for your help,
Andrew V. Romero




Reply With Quote
  #2  
Old   
Douglas Crockford
 
Posts: n/a

Default Re: Understanding this Code - 07-24-2003 , 09:13 AM






Quote:
1) var x = {}; // I think this creates a new object that is empty.

1. This creates a new array with nothing in it. I wouldn't do it this way
personally. I would do var x = new Array(); or to be more precise I would
probably call it arrQueryString or something like that.
{} is the best way to make a new empty object.
[] is the best way to make a new empty array.
Use [] if the subscripts are going to be integers.
Otherwise use {}.

http://www.crockford.com/javascript/survey.html



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.