Javascript Array

Creating an Object

We have already seen an example of an Array object being created. To create an Array object, we used the JavaScript statement
var myArray = new Array();
So how is this statement made up?
The first half of the statement is familiar to us. We use the var keyword to define a variable called myArray. This variable is initialized, using the assignment operator (=), to the right-hand side of the statement.
The right-hand side of the statement consists of two parts. First we have the keyword new. This tells JavaScript that we want to create a new object. Next we have Array(). This is the constructor for an Array object. It tells JavaScript what type of object we want to create. Most objects have constructors like this. For example, the Date object has the Date() constructor.


we can pass parameters to the constructor Array() to add data to our object. For example, to create an Array object that has three elements containing the data "Paul", "Paula", and "Pauline", we use
var myArray = new Array("Paul", "Paula", "Pauline");
Let's see some more examples, this time using the Date object. The simplest way of creating a Date object is
var myDate = new Date();
This will create a Date object containing the date and time that it was created. However,
var myDate = new Date("1 Jan 2000");
will create a Date object containing the date 1 January 2000.
How object data is stored in variables differs from how primitive data, such as text and numbers, is stored. (Primitive data is the most basic data possible in JavaScript.) With primitive data, the variable holds the data's actual value. For example
var myNumber = 23;
means that the variable myNumber will hold the data 23. However, variables assigned to objects don't hold the actual data, but rather a reference to the memory address where the data can be found. This doesn't mean we can get hold of the memory address—this is something only JavaScript has details of and keeps to itself in the background. All you need to remember is that when we say that a variable references an object, this is what we mean. We show this in the following example:
var myArrayRef = new Array(0, 1, 2);
var mySecondArrayRef = myArrayRef;
myArrayRef[0] = 100;
alert(mySecondArrayRef[0]);
First we set variable myArrayRef reference to the new array object, and then we set mySecondArrayRef to the same reference—for example, now mySecondArrayRef is set to reference the same array object. So when we set the first element of the array to 100 as shown here:
myArrayRef [0] = 100;
and display the contents of the first element of the array referenced in mySecondArrayRef as follows:
alert(mySecondArrayRef[0])
we'll see it also magically has changed to 100! However, as we now know, it's not magic; it's because both variables referenced the same array object because when it comes to objects, it's a reference to the object and not the object stored in a variable. When we did the assignment, it didn't make a copy of the array object, it simply copied the reference. Contrast that with the following:
var myVariable = "ABC";
var mySecondVariable = myVariable;
myVariable = "DEF";
alert(mySecondVariable);
In this case we're dealing with a string, which is primitive data type, as are numbers. This time it's the actual data that's stored in the variable, so when we do this:
var mySecondVariable = myVariable;
mySecondVariable gets its own separate copy of the data in myVariable. So the alert at the end will still show mySecondVariable as holding "ABC."
To summarize this section, we create a JavaScript object using the following basic syntax:
var myVariable = new ObjectName(optional parameters);

Related Posts:
  • Reserved Words in JavaScript Reserved Words in JavaScript 1.5 >abstract else instanceof switch >boolean enum int synchronized >break export interface this byte extends long throw case f… Read More
  • Creating Elements, Attributes, and Objects An important benefit of dynamic content is the ability to create new content from freshly received data. This is necessary in dynamic menu creation, navigation, breadcrumbs, and web services, among other applications. Ajax… Read More
  • JavaScript Refresh Page you refresh the page using document.location.reload(). You can add the true keyword to force the reloaded page to come from the server (instead of cache). Alternatively, you can use the false keyword to reload the page from … Read More
  • Expression Statements Assignment statements are one major category ofexpression statements.  For example: greeting = "Hello " + name;i *= 3; The increment and decrement operators, ++ and --, are relatedto assignment statements. These have … Read More
  • var keyword JavaScript The var and function are declaration statements—they declare or define variables and functions. These statements defineidentifiers (variable and function names) that can be used elsewherein your program and assign values to … Read More
  • HTTP “200 OK” response A complete request is not necessarily a successful request, andyour handler for the load event should check the status codeof the XMLHttpRequest object to ensure that you received anHTTP “200 OK” response rather than a “404 … Read More
  • Remote JavaScript The primary reason is that the round trip time required to submit a form and then download the response is often inconvenient. The user experience is much improved if, instead of clicking a Submit button and watching the … Read More
  • concat() Method The concat() method returns the array resulting from appending its arguments to  the array on which it was invoked. Given the script: var myArray = ["red", "green", "blue"]; alert(myArray.concat("cyan", "yello… Read More
  • JavaScript Object Literals Object literals are a popular way to create new objects with a specific set of properties, as opposed to explicitly creating a new instance of Object and then adding properties. For example, this pattern is rarely used: … Read More
  • The try-catch Statement JavaScript provides a try-catch statement that is capable of intercepting thrown errors before they are handled by the browser. The code that might cause an error comes in the try block and code that handles the error goes i… Read More
  • JavaScript Form Validation  JavaScript Form Validation. JavaScript can be used to validate data in HTML forms <script type="text/javascript"> function validate_all()     {     var frmReg=document.manageadmin… Read More
  • Function Call Spacing Almost universally, the recommended style for function calls is to have no space between the function name and the opening parenthesis, which is done to differentiate it from a block statement. For example: // Good doSom… Read More
  • JavaScript Dynamic Links and Menus Many sites use JavaScript to create links to other website pages. Here is some examplecode with different link types that you may want to avoid: <HTML><head><title>Link Examples ~ Things to stay away from&l… Read More
  • Embedding Content for Plug-Ins Although never officially a part of any HTML specification, the <<embed>> tag is most often used to include embedded objects for Netscape and Internet Explorer. A Macromedia Flash file might be embedded as fo… Read More
  • JavaScript Security Downloading and running programs written by unknown parties is a dangerous proposition. A program available on the Web could work as advertised, but then again it could also install spyware, a backdoor into your system, … Read More