chapter08
chapter08
Language
Fundamentals
Chapter 8
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8 cont.
9 10
Functions Object
Prototypes
11 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What is JavaScript & What Can It Do?
Client-Side Scripting
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What is JavaScript & What Can It Do?
JavaScript’s History
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What is JavaScript & What Can It Do?
JavaScript and Web 2.0
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What is JavaScript & What Can It Do?
JavaScript in Contemporary Software Development
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Where Does JavaScript Go?
Inline JavaScript
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Where Does JavaScript Go?
Embedded JavaScript
<script type="text/javascript">
/* A JavaScript Comment */
alert("Hello World!");
</script>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Where Does JavaScript Go?
External JavaScript
<head>
<script type="text/javascript" src="greeting.js"></script>
</head>
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Where Does JavaScript Go?
Users without JavaScript
• Web crawler
• Browser plug-in.
• Text-based client.
• Visually disabled client.
• The <NoScript> Tag
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Variables and Data Types
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Variables and Data Types
Example variable declarations and Assignments
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Variables and Data Types
Data Types
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Variables and Data Types
Reference Types
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
JavaScript Output
alert("Hello world");
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
JavaScript Output
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
JavaScript Output
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
JavaScript Output
Chrome JavaScript Console
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
JavaScript Output
Fun with document.write()
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Conditionals
If, else if, else
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Conditionals
switch
switch (artType) {
case "PT":
output = "Painting";
break;
case "SC":
output = "Sculpture";
break;
default:
output = "Other";
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Conditionals
Conditional Assignment
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Conditionals
Truthy and Falsy
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Loops
While and do . . . while Loops
var count = 0;
// do something
// ...
count++;
count = 0;
do {
// do something
// ...
count++;
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Loops
For Loops
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Arrays
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Arrays
object literal notation
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Arrays
Some common features
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Arrays
Arrays Illustrated
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8
1 2
JavaScript 1: Where Does
Language
JavaScript Go?
Fundamentals
3 Variables and
Data Types 4 JavaScript
Output
5 Conditionals
6 Loops
7 8
Arrays Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Objects
Object Creation—Object Literal Notation
var objName = {
name1: value1,
name2: value2,
// ...
nameN: valueN
};
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Objects
Object Creation—Object Literal Notation
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Objects
Object Creation—Constructed Form
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8 cont.
9 10
Functions Object
Prototypes
11 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Function Declarations vs. Function Expressions
function subtotal(price,quantity) {
return price * quantity;
}
The above is formally called a function declaration,
called or invoked by using the () operator
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Function Declarations vs. Function Expressions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Anonymous Function Expressions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Nested Functions
function calculateTotal(price,quantity) {
var subtotal = price * quantity;
return subtotal + calculateTax(subtotal);
// this function is nested
function calculateTax(subtotal) {
var taxRate = 0.05;
var tax = subtotal * taxRate;
return tax;
}
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Hoisting in JavaScript
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Callback Functions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Callback Functions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Objects and Functions Together
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Scope in JavaScript
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Scope in JavaScript
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Scope in JavaScript
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Function Constructors
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8 cont.
9 10
Functions Object
Prototypes
11 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Object Prototypes
There’s a better way
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Object Prototypes
Methods get duplicated…
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Object Prototypes
Using Prototypes reduces duplication at run time.
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Object Prototypes
Using Prototypes to Extend Other Objects
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 8 cont.
9 10
Functions Object
Prototypes
11 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Key Terms
ActionScript ES2015 libraries
Adobe Flash ES6 loop control variable
anonymous functions exception method
assignment expressions minification
AJAX external JavaScript files module pattern
applet falsy namespace conflict
arrays fail-safe design problem
arrow functions for loops objects
associative arrays functions object literal notation
browser extension function constructor primitive types
browser plug-in function declaration property
built-in objects function expression prototypes
callback function inline JavaScript reference types
client-side scripting immediately-invoked scope (local and global)
closure function strict mode
conditional assignment Java applet throw
dot notation JavaScript frameworks truthy
dynamically typed JavaScript Object Notation try. . . catch block
ECMAScript JSON undefined
embedded JavaScript lexical scope variables
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Questions?
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.