Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: General Javascript questions

  1. #1
    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.

  2. #2
    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

  3. #3
    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?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    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!

  5. #5
    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.

  6. #6
    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?

  7. #7
    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?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    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.
    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!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    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).
    - Mike

  10. #10
    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •