|
|
|
|
Lesson#29
|
History and Avolution of Internet
|
|
|
|
Who runs the Internet?
Who owns it?
Today’s Goal:
Introduction to the Internet
We looked at the role Internet
plays in today’s computing
We reviewed some of the history and evolution of the Internet
Next Lecture:
Internet Services
We will try to familiarize
ourselves with with some of the Internet services:
–http (surfing, shopping, searching)
–eMail
–ftp
–News groups, message boards, forums
–Instant messaging
–Multimedia delivery
function writeList( heading, words ) {
document.write( heading + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
}
29.3 Function Identifiers
The naming rules for function identifiers
are the same as were discussed for variable and array identifiers
29.4 Arguments of a Function
A comma-separated list of data
Arguments define the interface between the function and the rest of the
Web page
Arguments values are passed to the function by value (some popular
languages pass arguments ‘by
reference’ as well)
To ensure that a function is defined before it is called up, define all
functions in the HEAD portion of Web
pages
Keyword
Function
identifier
Pair of parenthesis
Function ‘arguments’ separated by
commas
Function definition
enclosed in a pair
of curly braces
function popUp( message ) {
window.alert( message ) ;
}
popUp( “Warning!” ) ;
A function call appearing
as a complete statement
function popUp( message ) {
window.alert( message ) ;
}
popUp( “Warning!” ) ;
function popUp( message ) {
window.alert( message ) ;
}
a = popUp( “Warning!” ) ;
document.write( a ) ;
What will get written by
this statement?
undefined
function add( a, b ) {
c = a + b ;
return c ;
}
sum = add( 2, 4 ) ;
document.write( sum ) ;
function add( a, b ) {
c = a + b ;
return c ;
}
document.write( add( 2, 4 ) ) ;
What would this
modifica-tion do?
function add( a, b ) {
c = a + b ;
return c ;
}
sum = add( 2, 4 ) ;
document.write( sum ) ;
What Would this Statement Do?
factorial( factorial ( 3 ) ) ;
This is termed as the
recursive
use of a function
Methods
Methods
are
functions
They are unusual
in the sense that they are stored as properties of
objects
Object
: A
named collection of properties (data,
state) & methods (instructions, behavior)
29.5 Event Handlers
Special-purpose functions that come
predefined with JavaScript
They are unusual
in the sense that they are many times called in
the HTML part of a Web page and not the
<SCRIPT> … </SCRIPT> part
More on event handlers in a future lecture
Predefined, Top-Level or Built-In Functions
Event handlers are not the only functions
that come predefined with JavaScript. There are many others.
Practically, there is no difference between predefined functions and
those that are defined by the
programmer (termed as user-defined or custom functions)
There are many of them, but here we discuss only two: parseInt( ),
parseFloat( )
parseInt( )
Syntax: parseInt ( string )
string1 = “3.14159” ;
prop
1
prop
2
prop
5
nam
e
prop
3
prop
4
A collection of
properties & methods
All objects have the “name”
property: it holds the name of
the object (collection)
method 3
method 1
method 2
The dictionary meaning of ‘Parse’: To
breakdown into simpler components and
analyze
document.write( parseInt( string1 )
) ;
document.write( “<BR>” ) ;
string2 = “$100.00” ;
document.write( parseInt( string2 ) ) ;
document.write( “<BR>” ) ;
string3 = “ 23 ” ;
document.write( parseInt( string3 ) ) ;
1. Parses the string argument; returns an integer
2. If it encounters a non-numeral - anything other than (+,-) or (0-9) -
it ignores it and all succeeding
characters, and returns the integer value parsed up to that point
3. If the first character cannot be converted to a number, parseInt
returns NaN
4. parseInt truncates numbers to integer values
5. Leading and trailing spaces are ignored
parseFloat( )
Syntax: parseFloat ( string )
string1 = “3.14159” ;
document.write( parseFloat( string1 ) ) ;
document.write( “<BR>” ) ;
string2 = “$100.00” ;
document.write( parseFloat( string2 ) ) ;
document.write( “<BR>” ) ;
string3 = “ 23E-15 ” ;
document.write( parseFloat( string3 ) ) ;
1. Parses the string argument; returns a FP number
2. If it encounters a character other than
A sign (+,-)
A numeral (0-9)
A decimal point
An exponent
it returns the value up to that point, ignoring that and all succeeding
characters
3. If the first character cannot be converted to a number, parseFloat
returns NaN
4. Leading and trailing spaces are ignored
29.6 Scope of Variable
Defining the space in which a variable is
effective is known as
defining the scope of a variable. A variable can be either
local
or global
in scope.
Local and Global Variables
Local or Function-level Variable
Effective only in the function in which
they are declared
Global Variables
Visible everywhere on the Web page
function factorial( n ) {
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write( “k = ”, k ) ;
document.write( “<BR>” ) ;
3
NaN
23
Example
document.write(n, "! = ", factorial( n
) ) ;
function factorial( n ) {
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write( “k = ”, k ) ;
document.write( “<BR>” ) ;
document.write(n, "! = ", factorial( n ) ) ;
function factorial( n ) {
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
10! = 3628800
k = 11
document.write(n, "! = ", factorial( n ) )
;
document.write( “<BR>” ) ;
document.write( “k = ”, k ) ;
function factorial( n ) {
var k ;
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write(n, "! = ", factorial( n ) ) ;
document.write( “<BR>” ) ;
document.write( “k = ”, k ) ;
‘k’ is a Local Variable
‘k’ is not declared or used in the main
code
Instead, it is declared within the function ‘factorial’
only
‘k’ i local to the ‘factorial’
function, and does not hold any meaning outside that function
function factorial( n ) {
var k, product ;
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write(n, "! = ", factorial( n ) ) ;
document.write( “<BR>” ) ;
document.write( product ) ;
Local Variables
Declaring variables (using the var keyword)
within a function, makes them
local•They are available only
within the function and hold no meaning outside of it
Global Variables
All other variables used in a Web page (or
window) are global
They can be manipulated from the main
code as well as from any of the functions
They include:
–All variables declared in the main code
–All variables used but not declared in the main code
–All variables used but not declared in any of the functions defined on
the Web page (or window)
function writeList( heading, words ) {
document.write( heading + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
}
words = new Array ( 10 ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
words[ k ] = window.prompt( "Enter word # " + k, "" ) ;
}
writeList( “Unsorted Words:”, words ) ;
words.sort( ) ;
writeList( “Sorted List:”, words ) ;
words.reverse( ) ;
writeList( “Reverse-Sorted List:”, words ) ;
Local –vs- Global
Global variables can make the logic of a
Web page difficult to understand
Global variables also make the reuse and maintenance of your code much
more difficult
Here ‘product’ has been made a local
variable as well
Would the functionality
change if we delete the
argument ‘words’ from these
4 places?
During Today’s Lecture …
We looked at functions and their use for
solving simple problems
We became familiar with a couple of JavaScript’s built-in functions
We became familiar with the concept of local and global variables
Next Web Dev Lecture:
Event Handling
We’ll learn to appreciate the concept of event
driven programming
We will produce solutions for simple problems using various event
handlers
HEURISTIC:
If it’s possible to define a variable as local,
do it!
var u ;
document.write( m ) ;
var c, d ;
x = y * y ;
r = s ;
var a, b ;
p = q + 2 ;
y
r
q d
x
s
p c
m b
u a
Glob Local
Variables declared within functions are
local; all others global
|
|
|
|