jschap02 - Chapter 2 Variables Functions Objects and Events...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Previewing the NorthAmericaImageMap.html File
Background image of page 2
Section A: Working with Variables, Functions, and Events
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Objectives In this section, you will learn how to: Declare and use variables Define functions Call functions Use JavaScript objects Use object inheritance and prototypes Use object methods About variable scope
Background image of page 4
Variables Values stored in computer memory locations are called variables In JavaScript, use the reserved keyword var to create variables Reserved words , or keywords , are part of the JavaScript syntax Reserved words cannot be used for variable names When you use the reserved word var to create a variable, you declare the variable
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
JavaScript Reserved Words
Background image of page 6
Variables You can declare multiple variables in the same statement using a single var keyword followed by a series of variable names and assigned values separated by commas The name assigned to a variable is an identifier Identifiers must begin with an uppercase or lowercase ASCII letter (ex. ($) or (_) Reserved words cannot be used for variable names, and there should be no spaces within a variable name
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Variables Common practices in variable names: Use an (_) to separate individual words Use a lowercase letter for the first letter of first word Variable names, like other JavaScript code, are case-sensitive
Background image of page 8
Image Map
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Defining Functions A function allows you to treat a related group of JavaScript statements as a single unit Lines that compose a function within an HTML document are called the function definition The syntax for defining a function is: function name_of_function (parameters) { statements; } A parameter , or argument , is a variable that will be used within a function
Background image of page 10
Function Definitions - Three Parts 1. The reserved word function followed by the function name. The reserved word function notifies the JavaScript interpreter that the code following is a function. As with variables,the name assigned to a function is called an identifier. The same rules and conventions that apply to variable names, apply to function names 2. Any parameters required by the function are contained within parentheses following the function name 3. The function’s statements, enclosed in curly braces { }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Function That Prints Name of Multiple Companies
Background image of page 12
Calling Functions A function definition does not execute automatically To execute a function, you must invoke, or call , it from elsewhere in the program Sending variables or values to a called function’s arguments is called passing arguments You are not required to return a value from a function Using unique names to identify specific variables makes it easier to understand a program’s logic and assist in the debugging process
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
JavaScript Function Being Called from the <BODY> Section
Background image of page 14
Output of the JavaScript Function Being Called from the <BODY> Section
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 50

jschap02 - Chapter 2 Variables Functions Objects and Events...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online