lecture04 - JavaScript Functions & Arrays Reading: [1]...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4 Reading: [1] Zeid, Ch. 20; [3] Sebesta, Ch. 4; [5] Deitel, Ch. 10-11 http://www.cs.man.ac.uk/~kechen/pages/teaching/comp17032
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP17032 Web Technology and Practice 1 2 Aims and Objectives • Aims Understand the basics of JavaScript functions and arrays, their definitions, their use, their input, their output, and the use of predefined JavaScript functions • Objectives – Importance of functions and arrays – Function definition and calling – Array definition and use – Array manipulation – Examples
Background image of page 2
COMP17032 Web Technology and Practice 1 3 Introduction • A function is a set of JavaScript statements that perform a designated task • Functions make programs modular and portable • One can create a function library to reuse some functions • Array is a complex variable that can hold multiple values • JavaScript uses arrays in a unique way • Arrays and functions must be defined before we can use them
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP17032 Web Technology and Practice 1 4 Function Definition • A function definition has two parts: Signature Æ specifies function name and (optionally) input parameters Body Æ includes any legal JavaScript statements •S i g n a t u r e : function functionName([param1, param2,…]) Parameters provide input to the function and are optional •B o d y : { statement 1; statement 2; return value; } Body is included between two curly brackets • A function definition is executed only when it is called
Background image of page 4
COMP17032 Web Technology and Practice 1 5 Function Calls • Function definitions have to be called to be executed • Function call if function returns a value: returnValue = functionName(arg1, arg2, …) • Function call if function does not return a value: functionName(arg1, arg2, …) • The call consists of function name and input values known as arguments or parameters • Arguments in a call must match those in the definition • Function call can be placed before or after the function definition • Function calls can be nested and there is no limit on level of nesting
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP17032 Web Technology and Practice 1 6 Example: Function definition • Listing 2-1 creates a form that uses a function to produce an alert box generated in response to a button click
Background image of page 6
COMP17032 Web Technology and Practice 1 7 Example Output • doSomething() function is called when button is clicked • this is peformed by onclick event handler (see later lecture)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP17032 Web Technology and Practice 1 8 Example: Passing arguments to functions • In Listing 2-2, a value is passed from onclick event handler in body to showText() function in head
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 33

lecture04 - JavaScript Functions & Arrays Reading: [1]...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online