JavaScript
  Home arrow JavaScript arrow Page 5 - A Custom 2D JavaScript Array
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

A Custom 2D JavaScript Array
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-10-06

    Table of Contents:
  • A Custom 2D JavaScript Array
  • The Array Properties and Methods
  • Object Type Array with its Contents
  • Object Type Array Without Contents
  • The Constructor Function with a Number of Rows and Columns

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Custom 2D JavaScript Array - The Constructor Function with a Number of Rows and Columns


    (Page 5 of 5 )

    We have a constructor function that has two parameters. The first one receives the number of rows; the second one receives the number of columns. Statements in the constructor functions create the rows (this[i]) and the cells (columns). Here we have a regular array with rows of the same length. The following code illustrates this:

    <html>

    <head>

    <script type="text/javascript">


    function CustomArray(y,x)

    {

    //form the row string

    rowStr = "{"

    for (j=0;j<y;j++)

    {

    if (j == 0)

    rowStr += (j + ":" + "'" + undefined + "'");

    else

    rowStr += ("," + j + ":" + "'" + undefined + "'");

    }

    rowStr += "}";


    //form the rows

    for (i=0;i<x;i++)

    {

    //form evaluation string

    evalStr = "this[i] = " + rowStr + ";";

    eval(evalStr);

    }

    }


    myArray = new CustomArray(6,5);

    myArray[2][3] = "val23";

     

    </script>

    </head>

    <body>

    <button type="button" onclick="alert(myArray[4][1]);">Click A</button>

    <button type="button" onclick="alert(myArray[2][3]);">Click B</button>

    </body>

    </html>

    The constructor function is CustomArray(y,x). The y parameter takes the arguments for the number of rows. The x parameter takes the argument for the number of columns (cells). The first code segment of this function forms the string for the object initializer for the rows. This is the same for all rows here. The code segment uses a for-loop for this. If there are five cells, each of them will be initially undefined.

    The second code segment forms the property for each row; it joins the string of the object initializer to the "this[i] = " string in order to form the evalStr variable. The JavaScript top level eval() function is used in this segment. It takes the evalStr as argument and produces the statement that defines the property of the constructor function (object type).

    The last code segment in the script forms the myArray array object of 6 rows and 5 columns. 6 and 5 are arguments of the constructor function. This code segment gives the value "val23" to the cell content of the cell at row 2, column 3.

    You have two HTML buttons. When clicked, the first one displays the content of the cell at row 4, column 1. This is undefined. The second one, on the other hand, will display the value "val23," which is the content of the cell at row 2, column 3.

    Let us take a break here. In the next part we shall look at the properties and methods.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek