1. (from Chapter 9 of the text). IT350 Web and Internet Programming. SlideSet #9:
JavaScript Functions. Function Definitions. • Syntax and terminology: function ...
new functions that the programmer writes with. “prepackaged” functions and
objects available in. JavaScript. • The term method implies that the function
belongs ...
Assignment 4 Javascript Guide. CIS*1000*DE. This tutorial will help you with the
creation of a simple calculator. The requirements for the assignment.
making functions function expression. › function(args) {body} functions are '
polymorphic'. › implicit typed. › depends on how args used. > three = function ...
More Regular HTML. . JavaScript. 4 www.corewebprogramming.com. A
Simple Script. .
Dynamic HTML (DHTML). Interactive ... Example. . .
function fact(n) { if (n==1) return n; ..... HTML. The definitive reference” O'Reilly.
Javascript â web pages with dynamic content. Page 4. Javascript. ⢠Programming Language used in web design. ⢠Unli
PHP â dynamically generated web pages. ⢠Javascript â web pages with dynamic content. Page 4. Javascript. ⢠Prog
May 15, 2004 ... 23. JavaScript Programming. Practices. In this chapter we bring to a close our
discussion of JavaScript by highlighting some recommended ...
Javascript is an interpreted language with a C like syntax. While many people ...
This article focuses on bringing people who already know another programming.
Online PDF JAVASCRIPT: Easy JavaScript Programming For Beginners. Your Step-By-Step Guide to Learning JavaScript Program
PDF Download Learning JavaScript: JavaScript Essentials for Modern Application Development Free Online, Download Learnin
2008 Pearson Education, Inc. All rights reserved. JavaScript: Events. 2. The
wisest prophets make sure of the event first. — Horace Walpole. Do you think I
can ...
Oct 18, 2013 - applications with JavaScript, but it is still important to understand ... JavaScript is loaded, objects a
Replacing the prototype property with a new object does not update former instances .....107 User-defined constructors can leverage the same ...
Javascript adalah bahasa skrip yang ditempelkan pada kode HTML dan ...
Javascript bukanlah bahasa Java dan merupakan dua bahasa yang berbeda.
As Technical Director for SitePoint, Kevin Yank keeps abreast of all that is new ...
PHP & MySQL,1 now in its third edition, Kevin also writes the SitePoint Tech ...
Oct 18, 2013 - Functions and Closures. â. AJAX. If you've been developing enterprise web applications, it's likely tha
JavaScript Functions. •A function is a major programming construct that you have
not yet seen — but, I'd argue, once you've seen it, you might wonder how you ...
What is jQuery? • A framework for Client Side JavaScript. • Frameworks provide
useful alternatives for common programming tasks, creating functionality which ...
JavaScript's LiveConnect âJava. â Java 6: javax.script. â Adobe PDF. â Adobe ActionScript based on JavaScript. Ajax Technology in Web Programming.
Learning JavaScript Design Patterns: A JavaScript and jQuery Developer s Guide PDF .... useful from the perspective of a
Mar 22, 2001 ... Chapter 3: JavaScript in Action. In this chapter, you get your first opportunity to
write JavaScript! This chapter introduces you to. JavaScript ...
Program Modules in JavaScript Programmer-Defined Functions Function Definitions Random-Number Generation Example: Game of Chance Another Example: Random Image Generator Scope Rules JavaScript Global Functions
1
10.1 Introduction • Functions – Started by function call – Receive necessary information via arguments (parameters) – Boss-Worker relationship • • • •
Calling function Called function Return value when finished Can have many tiers
10.2 Program Modules in JavaScript • Function calls – Name – Left parenthesis – Arguments separated by commas • Constants, variables or expressions
– Right parenthesis – Examples: total += parseFloat( inputValue ); total += parseFloat( s1 + s2 );
4
10.3 Programmer-Defined Functions • Defining functions – All variables declared in function are called local • Do not exist outside current function
– Parameters • Also local variables
– Promotes reusability • Keep short • Name clearly
5
10.4 Function Definitions • Format of a function definition function function-name( parameter-list ) { declarations and statements }
– Function name any valid identifier – Parameter list names of variables that will receive arguments • Must have same number as function call • May be empty – Declarations and statements • Function body (“block” of code) 6
10.4 Function Definitions • Returning control – return statement
– Can return either nothing, or a value return expression;
– No return statement same as return; – Not returning a value when expected is an error
7
10.4 Function Definitions • Writing a function to square two numbers – for loop from 1 to 10
– Pass each number as argument to square – return value of argument multiplied by itself – Display result
8
1
"1.0"?>
2
4 5
-->
6
-->
7 8 9 10
"http://www.w3.org/1999/xhtml"> A A ProgrammerProgrammer-Defined square Function Function
11 12
"text/javascript">
13
-->
31
The return statement passes the value of y * y back to the calling function.
32 33
34
10
10.4 Function Definitions Fig. 10.2
Using programmer-defined function square.
11
10.4 Function Definitions • Finding the maximum of 3 numbers – Prompt for 3 inputs – Convert to numbers – Pass to maximum – Math.max
12
1
"1.0"?>
2
4 5
-->
6
-->
7 8 9 10
Finding Finding the Maximum of Three Values Values
Prompt for the user to input three integers.
11 12
"text/javascript">
13
-->
38
y and z get the value of variables value1, value2 and value3, respectively.
39 40
41
42 43
Click
Click Refresh (or Reload) to run the script again
again
44
14
10.4 Function Definitions Fig. 10.3
Programmer-defined maximum function (1 of 2).
15
10.4 Function Definitions Fig. 10.3
Programmer-defined maximum function (2 of 2).
16
10.5 Random-Number Generation • Random-number generation introduces element of chance – Math.random
var randomValue = Math.random(); – Floating point value between 0 and 1 – Adjust range by scaling and shifting – Math.floor • Always round down
Math.floor( 1 + Math.random() * 6 )
17
1
"1.0"?>
2
4 5
-->
6
-->
RandomInt.html (1 of 2)
7 8 9 10
"http://www.w3.org/1999/xhtml"> Shifted Shifted and Scaled Random Integers Integers
11 12
"text/javascript">
13
-->
33
34 35
36
37 38
Click
Click Refresh (or Reload) to run the script again
again
39
19
10.5 Random-Number Generation Fig. 10.4
Random integers, shifting and scaling.
20
1
"1.0"?>
2
4 5
-->
6
-->
RollDie.html (1 of 3)
7 8 9 10
Roll Roll a SixSix-Sided Die 6000 Times Times
11 12
"text/javascript">
13
-->
61
62 63
64
65 66
Click
Click Refresh (or Reload) to run the script again
again
67
23
10.5 Random-Number Generation Fig. 10.5
Rolling a six-sided die 6000 times.
24
10.6 Example: Game of Chance • Craps – Click Roll Dice – Text fields show rolls, sum and point – Status bar displays results
25
10.6 Example: Game of Chance • Uses XHTML forms – Gather multiple inputs at once – Empty action attribute – name attribute allows scripts to interact with form
• Event handling and event-driven programming – Assign a function to an event – Onclick
• Constants – Variable that cannot be modified – Part of many languages, not supported in JavaScript • Name “constant” variables with all capital letters
– Make values easier to remember/change 26
10.6 Example: Game of Chance • Changing properties – Access with dot (.) notation – value property of text fields – status property of window
27
1
"1.0"?>
2
4 5
-->
6
-->
7 8 9 10
"http://www.w3.org/1999/xhtml"> Program Program that Simulates the Game of Craps Craps
11 12
"text/javascript">
13
-->
87
88 89
Referencing the names of form elements in the XHTML document, the values of the dice are placed in their respective form fields.
31
90 91
"">
92
"1">
93
Craps Craps Craps
94
Die
Die 1
1
95
96
97
Die
Die 2
2
98
99
101
102
103
Point
Point
Point
104
105
107
108
110
Craps.html (5 of 5)
Sum
Sum
Sum
100
109
Outline
111
32
10.6 Example: Game of Chance Fig. 10.6
Craps game simulation.
A text XHTML GUI component
A button XHTML GUI component
Browser’s status bar
33
10.6 Example: Game of Chance Fig. 10.6
Craps game simulation.
34
10.6 Example: Game of Chance Fig. 10.6
Craps game simulation.
35
10.7 Another Example: Random Image Generator • Randomly selecting an image – Images have integer names (i.e., 1.gif, 2.gif, …, 7.gif) – Generate random number in proper range – Update src property
36
1
"1.0"?>
2
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 5
-->
6
-->
RandomPicture.html (1 of 1)
7 8 9 10
"http://www.w3.org/1999/xhtml"> Random Random Image
Inserting a random number into the image’s src property with document.write and Math.random Generator Generator
11 12
"text/javascript">
13
-->
18
19 20
21 22 23 24
Click
Click Refresh (or Reload) to run the script again
again
25
37
10.7 Another Example: Random Image Generator Fig. 10.7
Random image generation using Math.random.
38
10.8 Scope Rules • Scope – Portion of program where identifier can be referenced – Inside function is local or function scope • Identifiers exist only between opening and closing braces • Local variables hide global variables
39
10.8 Scope Rules • Scope demonstration – Global variable x initialized to 1 – start has local variable x initialized to 5 – functionA has local variable x initialized to 25 – functionB has no local variable x – Observe output of each function
40
1
"1.0"?>
2
4 5
-->
Scoping.html (1 of 3)
--> -->
7 8 9 10
"http://www.w3.org/1999/xhtml"> A A Scoping Example Example
To begin the program, variable x is initialized to 1.
11 12
"text/javascript">
13
-->
52
Scoping.html (3 of 3)
+ "" );
Function functionB multiplies the value of x by 10.
53 54
55
"start()">
56
43
10.8 Scope Rules Fig. 10.8
Scoping example.
44
10.9 JavaScript Global Functions • Global object – Always available – Provides 7 methods – Do not need to explicitly reference Global before method call – Also holds all global variables, user defined functions
45
10.9 JavaScript Global Functions Global function escape
eval
isFinite
isNaN
Description This function takes a string argument and returns a string in which all spaces, punctuation, accent characters and any other character that is not in the ASCII character set (see Appendix D, ASCII Character Set) are encoded in a hexadecimal format (see Appendix E, Number Systems) that can be represented on all platforms. This function takes a string argument representing JavaScript code to execute. The JavaScript interpreter evaluates the code and executes it when the eval function is called. This function allows JavaScript code to be stored as strings and executed dynamically. This function takes a numeric argument and returns true if the value of the argument is not NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY; otherwise, the function returns false. This function takes a numeric argument and returns true if the value of the argument is not a number; otherwise, it returns false. The function is commonly used with the return value of parseInt or parseFloat to determine whether the result is a proper numeric value.
Fig. 10.9 JavaScript global functions.
46
10.9 JavaScript Global Functions Global function parseFloat
parseInt
unescape
Description This function takes a string argument and attempts to convert the beginning of the string into a floatingpoint value. If the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted value (e.g., parseFloat( "abc123.45" ) returns NaN, and parseFloat( "123.45abc" ) returns the value 123.45). This function takes a string argument and attempts to convert the beginning of the string into an integer value. If the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted value (e.g., parseInt( "abc123" ) returns NaN, and parseInt( "123abc" ) returns the integer value 123). This function takes an optional second argument, from 2 to 36, specifying the radix (or base) of the number. Base 2 indicates that the first argument string is in binary format, base 8 indicates that the first argument string is in octal format and base 16 indicates that the first argument string is in hexadecimal format. See see Appendex E, Number Systems, for more information on binary, octal and hexadecimal numbers. This function takes a string as its argument and returns a string in which all characters previously encoded with escape are decoded.