Go Back   Dynamic Drive Forums > General Coding > JavaScript
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-01-2006, 06:13 PM
Eclyps19 Eclyps19 is offline
Junior Coders
 
Join Date: Oct 2005
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default General Javascript questions

Okay. So i've been reading up, and I am going to be coming here with my random questions. Instead of making a new thread that can be answered in one post for every question i come across, I'm going to start posting them all in here.

Question #1 - Arrays

so you can make an array by using the following syntax
Code:
var a = new Array();
     a[0] = 1;
     a[1] = 2;
     a[2] = 3;
and so on.

First of all, if you try to find the lenth of the array, what will it tell you? Since you didn't define the length when you created the array, does it even have a length?

What if you try assigning something to like the 400th element in the array without assigning something to the other 399 elements? Will it go ahead and let you do that?

Thanks.
Reply With Quote
  #2  
Old 10-01-2006, 07:24 PM
blm126's Avatar
blm126 blm126 is offline
Senior Coders
 
Join Date: Sep 2005
Posts: 882
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by Eclyps19
First of all, if you try to find the lenth of the array, what will it tell you? Since you didn't define the length when you created the array, does it even have a length?
Yes it has a length. You can find it with the length property of the array. This property will update as you add more keys. Please not that the returned length starts at 1 while the keys start at 0.
Quote:
Originally Posted by Eclyps19
What if you try assigning something to like the 400th element in the array without assigning something to the other 399 elements? Will it go ahead and let you do that?
Yes
__________________
-Brady
Reply With Quote
  #3  
Old 10-01-2006, 09:05 PM
Eclyps19 Eclyps19 is offline
Junior Coders
 
Join Date: Oct 2005
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

so if i do a loop to display all elements from 0 - 400, it would display undefined up until 400?
Reply With Quote
  #4  
Old 10-01-2006, 10:37 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

It would output:

1
2
3
undefined
undefined
undefined
undefined
undefined

... and so on.

Another thing to note is that .length is the highest index in the array plus one. So:
Code:
var a = [];
a[0] = "a";
a[13] = "m";
a.length is 14, despite the fact that 1 through 12 are undefined.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #5  
Old 10-01-2006, 10:55 PM
Eclyps19 Eclyps19 is offline
Junior Coders
 
Join Date: Oct 2005
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ahhh i get it. thanks for clearing that up. More questions to come.
Reply With Quote
  #6  
Old 10-02-2006, 12:38 PM
Eclyps19 Eclyps19 is offline
Junior Coders
 
Join Date: Oct 2005
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Question #2 - Variable scopes

Say you are making a for loop.

Code:
for(i = 0; i < 10; ++i)
{
     documment.write(i);
}
this will work just fine, but what is the scope of the variable i? Since I didn't declare it using var, is it limited to the for loop? or can it be accessed outside of the loop?
Reply With Quote
  #7  
Old 10-02-2006, 03:16 PM
Eclyps19 Eclyps19 is offline
Junior Coders
 
Join Date: Oct 2005
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Question #3 - this. and document.

So when are each of these used?

How do I know that it should be document.bgColor = 'red'; instead of this.style.bgColor = 'red'; or something?
Reply With Quote
  #8  
Old 10-02-2006, 05:56 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

Quote:
what is the scope of the variable i?
Since you didn't use var, it is global unless it has been defined elsewhere.

A note on scope in Javascript: I know you are proficient with Java, and one common mistake people often make when moving to Javascript from Java and similar C-like languages is to assume that a code block denotes a new scope. This is not so in Javascript.
Quote:
Question #3 - this. and document.
document is the DOM reference to the root of the document (the <html> element). There's little that can be done directly with document, but it contains many properties and methods that are handy for altering the content of the page, such as body, forms[], images[], getElementById(), getElementsByTagName(), and the like.

this, on the other hand, refers to the object containing a method. Inside document.getElementById(), for example (if it were written in Javascript, that is) this would refer to document. If a method doesn't belong to an object, this refers to the global object (window in the case of browsers). Event handlers are properties of their respective elements, so inside an event handler, this usually refers to the element that caught the event.
Quote:
How do I know that it should be document.bgColor = 'red'; instead of this.style.bgColor = 'red'; or something?
The property I assume you mean, document.body.bgColor, is deprecated in favour of the CSS-compliant document.body.style.backgroundColor.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #9  
Old 10-02-2006, 08:21 PM
mburt's Avatar
mburt mburt is offline
Elite Coders
 
Join Date: Jul 2006
Location: Canada
Posts: 2,507
Thanks: 5
Thanked 22 Times in 22 Posts
Default

Quote:
How do I know that it should be document.bgColor = 'red'; instead of this.style.bgColor = 'red'; or something?
Document refers to the root of the document (DOM). "this." refers to the element that the event handler is coming from (event.srcElement).
Reply With Quote
  #10  
Old 10-07-2006, 01:56 PM
Eclyps19 Eclyps19 is offline
Junior Coders
 
Join Date: Oct 2005
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Question #4 - Adding .js files.

So I made a .js file to find imaginary numbers using a C++ program I made as a reference. I'm pretty sure it all works. Only problem is I am not really sure how to add it to the main page. I need something like an #include but for javascript.

And when I want to test it out, I can use it just like any other function, right?
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Forum Jump


All times are GMT. The time now is 12:51 AM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.