Simplifying Buttons through User-Defined Functions and Abstraction - Prof. Linda P. Duhadw, Study notes of Computer Science

The importance of simplifying complex buttons using user-defined functions and abstraction in javascript. It explains how functions reduce code length and complexity, and provides examples of defining and using simple user-defined functions. Additionally, it introduces the concept of abstraction and how it helps us focus on the big picture by ignoring unimportant details.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-cvk-1
koofers-user-cvk-1 🇺🇸

4

(1)

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3/18/2009
1
Simplifying buttons
consider the button from greetbox.html:
the size of the onclick attribute makes the button complex
and difficult to read
plus, must be careful with nested quotes ("…" vs. '…')
Simplifying buttons
functions provide a mechanism for simplifying complex
buttons such as this
recall from Chapter 5:
we used predefined JavaScript functions to accomplish
some of the work that needed to be done
Math.random
Math.floor
functions reduce the length and complexity of code
e.g., a single call to Math.sqrt replaces the underlying
complex algorithm
Simple user-defined functions
in addition to JavaScript's predefined functions, the user
can define new functions in the HEAD section and call
them within the page
Simple user-defined functions
a function definition begins with the word function
followed by its name and ()
a function name should be descriptive of the task being
performed
lines beginning with // are comments that describe the
function's behavior
comments are ignored by the interpreter, but make code
more user-readable
the statements to be executed when the function is
called are placed between the curly braces
the code from the
button is moved to the
user-defined Greet
function
as a result, the button
is greatly simplified
GENERAL RULE: if
more than one
statement is to be
associated with a
button, define a
separate function
Chapter 9
Abstraction and User-Defined Functions
pf2

Partial preview of the text

Download Simplifying Buttons through User-Defined Functions and Abstraction - Prof. Linda P. Duhadw and more Study notes Computer Science in PDF only on Docsity!

Simplifying buttons

 consider the button from greetbox.html:

 the size of the onclick attribute makes the button complex

and difficult to read

 plus, must be careful with nested quotes ("…" vs. '…')

Simplifying buttons

 functions provide a mechanism for simplifying complex

buttons such as this

 recall from Chapter 5:

 we used predefined JavaScript functions to accomplish

some of the work that needed to be done

 Math.random  Math.floor

 functions reduce the length and complexity of code

 e.g., a single call to Math.sqrt replaces the underlying complex algorithm

Simple user-defined functions

 in addition to JavaScript's predefined functions, the user

can define new functions in the HEAD section and call

them within the page

Simple user-defined functions

 a function definition begins with the word function

followed by its name and ()

 a function name should be descriptive of the task being

performed

 lines beginning with // are comments that describe the

function's behavior

 comments are ignored by the interpreter, but make code

more user-readable

 the statements to be executed when the function is

called are placed between the curly braces

 the code from the button is moved to the user-defined Greet function  as a result, the button is greatly simplified

GENERAL RULE: if more than one statement is to be associated with a button, define a separate function

Chapter 9

Abstraction and User-Defined Functions

Abstraction

 abstraction is the process of ignoring minutiae and focusing

on the big picture

 in modern life, we are constantly confronted with complexity  we don't necessarily know how it works, but we know how to use it e.g., how does a TV work? a car? a computer?

 we survive in the face of complexity by abstracting away

details

 to use a TV/car/computer, it's not important to understand the inner workings  we ignore unimportant details and focus on those features relevant to using it  e.g., TV has power switch, volume control, channel changer, …

Abstraction

 JavaScript functions (like Math.sqrt) provide

computational abstraction

 a function encapsulates some computation & hides the

details from the programmer

 the programmer only needs to know how to call the

function, not how it works

General Function Form

 to write general-purpose functions, we can extend

definitions to include:

 parameters  local variables  return statements

General Function Form

 parameters are variables that correspond to the function’s

inputs (if any)

 parameters appear in the parentheses, separated by commas

 local variables are temporary variables that are limited to that

function only

 if require some temporary storage in performing calculations, then declare local variables using the keyword var, separated by commas  a local variable exists only while the function executes, so no potential conflicts with other functions

 a return statement is a statement that specifies a value to be

returned from the function

 consists of the keyword return followed by a variable or expression

General Function

 We will be working with functions that get

information from text boxes or text areas

 We will be working with functions that display

their results in text boxes or text areas

function FUNCTION_NAME(){

//Description of what the function does

STATEMENTS_TO_PERFORM_DESIRED_TASK

Examples

 Let's try it