Docsity

Javascript - Software Engineering for the World Wide Web - Slides | SWE 642, Study notes of Software Engineering

Partial preview of the text

Download Javascript - Software Engineering for the World Wide Web - Slides | SWE 642 and more Study notes Software Engineering in PDF only on Docsity!

Software Engineering for

the World Wide Web

JavaScript

Dr. Vinod Dubey SWE 642 – Fall 2011 George Mason University

Acknowledgement

This lecture has been adapted from chapters 7, 8, 9, and 10 slides for: Internet & World Wide Web How to Program by P.J. Deitel and H.M. Deitel

Introduction

  • JavaScript introduces client-side scripting, which makes Web pages more dynamic and interactive
  • Helps enhance the functionality and appearance of web pages
  • The de facto standard for client-side scripting language for Web-based applications due to its highly portable nature

Introduction

  • JavaScript is not Java
  • JavaScript is a scripting language :
    • Embedded in HTML
    • Interpreted as the page is loaded
    • Can manipulate the HTML page
  • Is executed on the client (client side)

Simple Program: Displaying a Line of Text in a Web Page

  • Browser’s document object represents the

XHTML document currently being displayed in

the browser

  • Allows a script programmer to specify XHTML text to be displayed in the XHTML document
  • Browser contains a complete set of objects
  • that allow script programmers to access and manipulate every element of an XHTML document

Simple Program: Displaying a Line of Text in a Web Page (Cont.)

  • The document object’s writeln method
    • Writes a line of XHTML text in the XHTML document
    • Text displayed is dependent on the contents of the string written
    • Browser will interpret the XHTML elements as it normally does to render the final text in the document

Variables in JavaScript

  • A variable is a location in the computer’s memory where

a value can be stored for use by a program

  • Keyword var is used to declare the names of variables
  • All variables have a name, type and value
    • should be declared with a var statement before they are used in a program

Variables

  • A variable name can be any valid identifier consisting

of letters, digits, underscores ( _ ) and dollar signs ( $ )

  • that does not begin with a digit and
  • is not a reserved JavaScript keyword.
  • Declarations end with a semicolon ( ; )
  • Can be split over several lines, with each variable in the declaration separated by a comma
  • Several variables may be declared in one declaration or in multiple declarations.

More on Variables

  • When a variable is declared in JavaScript, but is not given a value, it has an undefined value. - Attempting to use the value of such a variable is normally a logic error.
  • When variables are declared, they are not assigned default values, unless specified otherwise by the programmer. - To indicate that a variable does not contain a value, you can assign the value null to it.

Keywords and Variables in JavaScript

  • Keywords are words with special meaning in JavaScript
  • Keywords can not be used as identifiers
  • Keyword var
    • Used to declare the names of variables

The Assignment Operator (=)

  • A variable is assigned a value with an assignment statement, using the assignment operator, =.
  • The = operator is called a binary operator, because it has two operands.

Comments in JavaScript

  • Comments
    • A single-line comment begins with the characters // and terminates at the end of the line
    • Comments do not cause the browser to perform any action when the script is interpreted; rather, comments are ignored by the JavaScript interpreter
    • Multiline comments begin with delimiter /* and end with delimiter */

Control Structures

  • Specify the order in which statements are to be executed in a computer program
  • Sequential execution
    • Execute statements in the order they appear in the code
  • Transfer of control
    • Changing the order in which statements execute
  • All programs can be written in terms of only three control structures - sequence - selection - repetition
  • Single-entry/single-exit sequence, selection and repetition structures