JavaScript Reference Card - Lecture Slides | CSC 1020, Exams of Computer Science

Material Type: Exam; Class: Computing and the Web; Subject: Computer Science; University: Villanova University; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/16/2009

koofers-user-gtu
koofers-user-gtu 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Addison-Wesley’s
JavaScript Reference Card
Kathleen M. Goelz and Carol J. Schwartz, Rutgers University
,!7IA3C1-dcahfj!:t;K;k;K;k
ISBN 0-321-32075-1
Javascript: A scripting language designed to be integrated
into HTML code to produce enhanced, dynamic, interac-
tive web pages.
DATA TYPES
Definition: The classification of values based on the specific
categories in which they are stored.
Primitive Types: String, Boolean, Integer, Floating Point,
Null, Void
Composite Types: Object, Array, Function. Composite data
types are in separate sections of the code.
NUMERIC
Integer: Positive or negative numbers with no fractional
parts or decimal places.
Floating Point: Positive or negative numbers that contain a
decimal point or exponential notations.
String: A sequence of readable characters or text, surround-
ed by single or double quotes.
Boolean: The logical values True/False, etc. used to com-
pare data or make decisions.
Null: The variable does not have a value; nothing to report.
Null is not the same as zero, which is a numeric value.
Casting: Moving the contents of a variable of one type to a
variable of a different type. You don’t move the contents to
a different variable; it stays in the same variable but the
data type is changed or “re-cast”.
VARIABLES
Definition: A placeholder for storing data. In JavaScript, a
declaration statement consists of the reserved word var and
the name (identifier) of one or more variables.
Format:
var variable_name
[var command is used to declare (create) variables]
Examples:
var myHouseColor
var myAddress
var vacation_house, condominium,
primaryResidence
Rules for Naming Variables:
1. Variables cannot be reserved words.
2. Variables must begin with a letter or underscore and
cannot begin with symbols, numbers, or arithmetic
notations.
3. Spaces cannot be included in a variable name.
Hints:
1. Although variables in JavaScript can be used without
being declared, it is good programming practice to
declare (initialize), all variables.
2. Variable names are case sensitive; for example Xdoes
not equal x.
javascript_reference_card.qxd 8/13/04 11:41 AM Page 1
pf3
pf4
pf5

Partial preview of the text

Download JavaScript Reference Card - Lecture Slides | CSC 1020 and more Exams Computer Science in PDF only on Docsity!

Addison-Wesley’s

JavaScript Reference Card

Kathleen M. Goelz and Carol J. Schwartz, Rutgers University

,!7IA3C1-dcahfj!:t;K;k;K;k

ISBN 0-321-32075-

Javascript : A scripting language designed to be integrated into HTML code to produce enhanced, dynamic, interac- tive web pages.

DATA TYPES

Definition : The classification of values based on the specific categories in which they are stored.

Primitive Types : String, Boolean, Integer, Floating Point, Null, Void

Composite Types: Object, Array, Function. Composite data types are in separate sections of the code.

NUMERIC

Integer: Positive or negative numbers with no fractional parts or decimal places.

Floating Point : Positive or negative numbers that contain a decimal point or exponential notations.

String : A sequence of readable characters or text, surround- ed by single or double quotes.

Boolean : The logical values True/False, etc. used to com- pare data or make decisions.

Null: The variable does not have a value; nothing to report. Null is not the same as zero, which is a numeric value.

Casting : Moving the contents of a variable of one type to a variable of a different type. You don’t move the contents to a different variable; it stays in the same variable but the data type is changed or “re-cast”.

VARIABLES

Definition : A placeholder for storing data. In JavaScript, a declaration statement consists of the reserved word var and the name (identifier) of one or more variables. Format : var variable_name [ var command is used to declare (create) variables] Examples : var myHouseColor var myAddress var vacation_house, condominium, primaryResidence Rules for Naming Variables :

  1. Variables cannot be reserved words.
  2. Variables must begin with a letter or underscore and cannot begin with symbols, numbers, or arithmetic notations.
  3. Spaces cannot be included in a variable name. Hints :
  4. Although variables in JavaScript can be used without being declared, it is good programming practice to declare (initialize), all variables.
  5. Variable names are case sensitive; for example X does not equal x.

IF-ELSE Statement: A conditional branching statement that includes a path to follow if the condition is TRUE and a path to follow if the condition is FALSE. Format : if (condition) { statements if condition is TRUE; } else { statements if condition is FALSE; } Example : if (score >= 65) { grade = “Pass”; message = “Congratulations”; } else { grade = “Fail” message = “Try again”; }

IF-ELSE IF Statement: A conditional branching statement that allows for more than two possible paths. The first time a true condition is encountered, the statement is executed and the remaining conditions will not be tested. Format: if (condition) { Statements if condition is TRUE; } else if (condition) { Statements if condition is TRUE; } else { Statements if no prior condition is true; }

INITIALIZING VARIABLES

Use the declaration statement to assign a value to the vari- able. The value is on the right of the equal sign; the variable is on the left.

Format :

var variable_name = value

Examples :

var myHouseColor = “yellow” [literal string value yellow assigned to variable myHouseColor] var myAddress = 473 [numeric value 473 assigned to variable myAddress] var bookTitle = “Time Capsule”, cost = 28.95, publisher = “Tucker Bay” [multiple variables can be assigned in one statement]

DECISION MAKING AND

CONTROL STRUCTURES

Definition : Statements and structures used to change the order in which computer operations will occur.

Types :

Conditional Branching IF, IF-ELSE, IF-ELSE IF, SWITCH, WHILE, DO, FOR

CONDITIONALS

IF Statement: A conditional branching statement used to determine whether a stated condition is TRUE.

Format :

if (condition) { statements if condition is TRUE }

Example :

if (score >= 65”) { grade = “Pass”; message = “Congratulations”; }

Initializing Arrays : Array items can be treated as simple variables: days[0] = “Sunday”; days[1] = “Monday”; etc.

STRING OBJECT

Definition: String object is created by assigning a string to a variable, or by using the new object constructor. Example: var name = “Carol”; var name = new String(“Carol”); Properties: Length: returns the number of characters in the string Prototype: allows the user to add methods and properties to the string Methods: String formatting methods (similar to HTML formatting tags) String.big String.blink String.italics Substring methods (allow user to find, match, or change patterns of characters in the string) indexOf() charAt() replace()

MATH OBJECT

Definition: Math object allows arithmetic calculations not supported by the basic math operators. Math is a built-in object that the user does not need to define. Examples: Math.abs(number) returns absolute value of the numeric argument Math.cos(number) returns the cosine of the argument, in radians Math.round(number) rounds number to the nearest integer

DATE/TIME OBJECTS

Date object provides methods for getting or setting infor- mation about the date and time. Note: Dates before January 1, 1970 are not supported.

WHILE LOOP:

Format:

while (condition) { Statements; Increment/decrement; }

Example:

var i = 0; while (i<=10) { document.write (“This is line “ + i); i++; }

Hint: Watch out for infinite loops, which do not have a stopping condition or have a stopping condition that will never be reached.

OBJECTS

Definition: Objects are a composite data type which con- tain properties and methods. JavaScript contains built-in objects and allows the user to create custom objects.

Creating Objects: Use the new constructor

var X = new Array()

Examples:

date, time, math, strings, arrays

ARRAY OBJECT

Definition: Array object is a variable that stores multiple val- ues. Each value is given an index number in the array and each value is referred to by the array name and the index number. Arrays, like simple variables, can hold any kind of data. You can leave the size blank when you create an array. The size of the array will be determined by the number of items placed in it.

Format:

var arrayname = new Array(size)

Hint: When you create an array, you create a new instance of the array object. All properties and methods of the array object are available to your new array.

Example:

var days = new Array (7) This creates an array of seven elements using the array constructor. The first item is days[0], the last item is days[6].

FUNCTIONS

Definition: A pre-written block of code that performs a specific task. Some functions return values; others perform a task like sorting, but return no value. Function names follow the same rules as variables names. There may or may not be an argument or parameter in the parenthesis, but the parenthesis has to be there.

User-defined Functions:

Example:

ParseInt() or ParseFloat() convert a string to a number.

To create a function:

Format:

function name_of_function (arguments) { statements to execute when function is called; }

Example:

function kilosToPounds (){ pounds=kilos*2.2046; }

This new function takes the value of the variable kilos, multiplies it by 2.2046, and assigns the result to the vari- able pounds.

To call a function: Give the name of the function followed by its arguments, if any

ParseInt(X); converts the data stored in the variable X into a numeric value. kilosToPounds(17); converts 17 kilos to the same mass in pounds, returning the value 37.4782.

METHODS

Definition : A special kind of function used to describe or instruct the way the object behaves. Each object type in JavaScript has associated methods available.

Examples :

array.sort(); document.write(); string.length();

Calling: To call or use a method, state the method name followed by its parameters in parentheses.

Example:

document.write(“Hello, world!”);

PUTTING IT TOGETHER:

JAVASCRIPT AND HTML

ON THE WEB

Cookies : Text-file messages stored by the browser on the user’s computer Purpose : To identify the user, store preferences, and present customized information each time the user visits the page Types : Temporary (transient, session) — stored in temporary memory and available only during active browser session Persistent (permanent, stored) — remain on user’s comput- er until deleted or expired

Browser Detection : A script written to determine which browser is running; determine if the browser has the capabili- ties to load the webpage and support the javascript code; and, if needed, load alternate javascript code to match the browser and platform.

Sniffing : A script written to determine whether a specific browser feature is present; i.e., detecting the presence of Flash before loading a webpage.

Event Handling: Use HTML event attributes (mouseover, mouse click, etc.) and connect event to a JavaScript function called an event handler