Docsity

JavaScript: Arrays and Objects, Study notes of Computer Science

Toggle sidebar
bg1
1
JavaScript (Arrays)
Image you need to keep track of the scores of students in a class
and you have 50 students
Declaring and handling 50 variables is not an easy task
Arrays come to the rescue
Array – Collection of values that can be treated as a unit and
where you can handle each individual entry (similar to an egg
carton)
You can visualize an array as a set of variables one after another
There are several ways to define arrays.
var scienceScores = new Array();
// Creates an empty array
var mathScores = new Array(3);
// Creates an array with 3 entries
var englishScores = [77, 88, 65];
// Creates an array with 3 entries
// having the specified values
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download JavaScript: Arrays and Objects and more Study notes Computer Science in PDF only on Docsity!

JavaScript (Arrays)



Image you need to keep track of the scores of students in a classand you have 50 students



Declaring and handling 50 variables is not an easy task



Arrays come to the rescue



Array – Collection of values that can be treated as a unit andwhere you can handle each individual entry (similar to an eggcarton)



You can visualize an array as a set of variables one after another



There are several ways to define arrays. var scienceScores = new Array();

// Creates an empty array

var mathScores = new Array(3);

// Creates an array with 3 entries

var englishScores = [77, 88, 65];

// Creates an array with 3 entries// having the specified values

JavaScript (Arrays)



To access elements of an array 

Use the [ ] operator



Integer Index values

starting at zero

will represent

each element



Accessing array elements mathScores[0] = 70;

// Assigning 70 to the first array element

mathScores[1] = 80;

// Assigning 80 to the second array element

var total = mathScores[0] + mathScores[1];

// reading the first

// and second elements

JavaScript (Objects) 

Property – Entity with a name and a value



In JavaScript an object is a composite type whichaggregates several properties



Object properties work like variables and fundamentallyare variables. You can assign values to them and readvalues from them



A property value can be any data type we have seen,including objects

JavaScript (Objects)



An example of an object is the

navigator

object which provides information

about the web browser



Among the

navigator

object properties you can find



appName – web browser name



appCodeName code name for the browser



You can access properties by specifying the object name, a period, and theproperty name or by specifying the property name using array-like syntax

navigator.appNamenavigator[“appName”]



Notice that you can read of modify properties using the above notation



Example 3

(See Example3.html)

JavaScript (Example)



By making use of functions, object properties and formswe can now define an html document with JavaScriptthat generates customized multiplication tables



Example 5

(See Example5.html)



Notice how we can pass properties of elements of theform to the multiplicationTable function

JavaScript (Window Object methods)



A

method is a function that is invoked through an object.



The window object has several interesting methods 

window.open() – creates and opens a window



window.print() – equivalent to clicking on thebrowser’s Print button



window.back() – equivalent to clicking on the browser’s Back button



window.forward() – equivalent to clicking on thebrowser’s Forward button

JavaScript (Examples on the Web



Let’s take a look at some JavaScript availableon the web



Set 1 http://www.bodo.com/javacool.htm



Set 2 http://www.crowes.f9.co.uk/Javascript/index.htm