Title: JavaScript Objects, Variables, and DOM
1JavaScript Objects,Variables, and DOM
2Variables
3Basics of a Variable
- Defines a memory location.
- Stores data.
- Value can change as script runs.
Jane
Fred
Susie
4Characteristics of a Variable
String
Jane
5Variable Lifecycle
Declare
Initialize
Use
Destroy
6Data Type
- Defines values and operations permitted.
- Types
- String.
- Number integer, floating point (decimal),
- Boolean.
- Array.
- Object.
7Names, Types, Values
A Number 123.59
ANumber 123.59
floating point
string
ANumber 123.59
string
ANumber Hello World!
string
curStatus false
curStatus false
Boolean
8Declaring and Using a Variable
ltscript type"text/javascript"gt var keepTrack
'Start' alert(keepTrack) keepTrack
'100' alert(keepTrack) lt/scriptgt
9Local Variable
ltscript type"text/javascript"gt function
myFunction1() var aVar 1 alert('myFunction1
' aVar) function myFunction2() alert('myF
unction2 ' aVar) lt/scriptgt
10Global Variable
ltscript type"text/javascript"gt var aVar
1 function myFunction1() alert('myFunction1
' aVar) function myFunction2() alert('myFu
nction2 ' aVar) lt/scriptgt
11Basic Operations
- Addition (numbers and strings).
- Subtraction.
- Multiplication.
- Division.
12Returned Values
13Some JavaScript User Interface Commands
- alert(message) no returned value.
- prompt(prompt, default)
- confirm(prompt)
window object methods.
14Function/Method Returns Value
variable function(parameters)
variable object.method(parameters)
variable prompt(prompt,default)
variable confirm(prompt)
15Examples
grossPay computeGross(hours, rate)
userName prompt(What is your name?,)
setAsHome confirm(Set as home page?)
16JavaScript Objects
17Review of Objects
18Using Objects
object.property
object.method()
object.method(param1, param2, )
parentobject.childobject.method()
19A Simple Accumulator Object
Properties
Methods
currentValue
increase(amount)
decrease(amount)
current()
20Using the Accumulator Object
currentValue
Out
accumulator.currentValue 3
3
accumulator.increase(5)
8
accumulator.current()
8
accumulator.decrease(4)
4
accumulator.currentValue
4
21JavaScript Document Object Model (DOM) Basics
window
document
location
screen
navigator
history
22Reminder
object
properties
methods
Support varies by browser type and browser
version.
DOM references identify properties and methods.