Home arrow JavaScript arrow Page 4 - Properties and Methods of Custom 2D JavaScript Arrays

Properties and Methods of Custom 2D JavaScript Arrays

This is part two of a series that discusses custom 2D JavaScript arrays. In this part of the series, we see how to develop the properties and methods of our custom 2D array.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
October 14, 2009
  1. · Properties and Methods of Custom 2D JavaScript Arrays
  2. · The Constructor Function
  3. · The Array Object Type Methods
  4. · Adding Cells

print this article

Properties and Methods of Custom 2D JavaScript Arrays - Adding Cells
(Page 4 of 4 )

After adding a row, the row is empty. You do not really need any function to add a cell. Rows are object initializers. By just assigning a value to any cell, you create (add) a cell with its value.

Let us look at an example. Consider the CustomArray() array object type that we have created. Assume that you have created the myArray object from CustomArray(). Assume that the row you inserted is at index 2. Below the constructor function in the code, you can type,

myArray[2][3] = “val23”;

The row you have just inserted is blank and is at row index 2. This statement creates a cell at the position of cell index 3 in the row, and gives it the value “val23.”

Note that you cannot add a cell like this to a row that does not exist or has not been inserted. If you want to add a cell to a row that is not in the array, first insert the row as shown above. Remember that the method of insertion shown above can be used to insert a row outside the range of rows that are already there.

Assigning Values

You can assign a value to any cell in any row that already exists in the array. If you want to assign a value to a cell in a row that does not exist, you have to insert that row first, and then you assign a value to any cell in the inserted row. When you assign a value to any cell in the inserted row, the cell is automatically created. If the cell was already there, the value (content) is replaced. A statement like the following will either create a new cell and its value in any row that exists, or have the value of the identified cell replaced:

myArray[4][1] = “val23”;

Having Other Properties and Methods

You can give a property to the array object type, as we gave the height property. You can give a method as we gave the insertRow method. If you want, for example, a method to sort the data in a column, you need to know how to write a sort function first; then you type the function into the constructor function as we typed the insertRowFn(i) function above. You make this function a method by declaring a method, as we did for the insertRow method.

Always remember that your 2D array object type is an object of object constructor function. So you give properties and methods as you would to any object.

Follow the link below for the complete code.

Custom 2D JavaScript Array.ZIP

Thanks for reading this two-part series.

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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials