Docsity

JavaScript: An Overview and Guide for Programmers, Slides of Javascript programming

Toggle sidebar
bg1
4.1 Overview of JavaScript
- Originally developed by Netscape, as LiveScript
- Became a joint venture of Netscape and Sun in
1995, renamed JavaScript
- Now standardized by the European Computer
Manufacturers Association as ECMA-262
(also ISO 16262)
-We’ll call collections of JavaScript code scripts,
not programs
- JavaScript and Java are only related through
syntax
- JavaScript is dynamically typed
-JavaScript’s support for objects is very different
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download JavaScript: An Overview and Guide for Programmers and more Slides Javascript programming in PDF only on Docsity!

4.1 Overview of JavaScript

  • Originally developed by Netscape, as LiveScript
  • Became a joint venture of Netscape and Sun in 1995, renamed JavaScript
  • Now standardized by the European Computer Manufacturers Association as ECMA- 262 (also ISO 16262)
  • We’ll call collections of JavaScript code scripts , not programs
  • JavaScript and Java are only related through syntax - JavaScript is dynamically typed - JavaScript’s support for objects is very different

4.1 Overview of JavaScript (continued)

  • JavaScript can be used to replace some of what is typically done with applets (except graphics)
  • JavaScript can be used to replace some of what is done with CGI (but no file operations or networking)
  • User interactions through forms are easy
  • The Document Object Model makes it possible to support dynamic HTML documents with JavaScript - Event-Driven Computation ()
    • User interactions with HTML documents in JavaScript use the event-driven model of computation - User interactions with form elements can be used to trigger execution of scripts

4.3 General Syntactic Characteristics

  • For this book, all JavaScript scripts will be embedded in HTML documents
  • Either directly, as in
- Or indirectly, as a file specified in the **src** attribute of ****
  • Identifier form : begin with a letter or underscore, followed by any number of letters, underscores, and digits - Case sensitive
  • 25 reserved words, plus future reserved words
  • Comments: both // and / … /

4.3 General Syntactic Characteristics

(continued)

  • Scripts are usually hidden from browsers that do not include JavaScript interpreters by putting them in special comments ****
  • Semicolons can be a problem
    • They are “somewhat” optional
    • Problem : When the end of the line can be the end of a statement – JavaScript puts a semicolon there

4.4 Primitives, Operations, &

Expressions

  • All primitive values have one of the five primitive types: Number, String, Boolean, Undefined, or Null

4.4 Primitives, Operations, &

Expressions (continued)

  • Boolean values are true and false
  • The only Null value is null
  • The only Undefined value is undefined
  • JavaScript is dynamically typed – any variable can be used for anything (primitive value or reference to any object) - The interpreter determines the type of a particular occurrence of a variable
  • Variables can be either implicitly or explicitly declared var sum = 0, today = "Monday", flag = false;

4.4 Primitives, Operations, &

Expressions (continued)

  • Numeric operators - ++ , -- , + , - , ***** , / , %
    • All operations are in double precision
    • Same precedence and associativity as Java
  • The Math Object provides floor , round , max , min , trig functions, etc. e.g., Math.cos(x)
  • The Number Object
    • Some useful properties: MAX_VALUE , MIN_VALUE , NaN , POSITIVE_INFINITY , NEGATIVE_INFINITY , PI - e.g., Number.MAX_VALUE - An arithmetic operation that creates overflow returns NaN
      • NaN is not == to any number, not even itself
      • Test for it with isNaN(x)
    • Number object has the method, toString

4.4 Primitives, Operations, &

Expressions (continued)

  • The typeof operator
    • Returns "number" , "string" , or "boolean" for primitives; returns "object" for objects and null
  • Assignment statements – just like C++ and Java
  • The Date Object
    • Create one with the Date constructor (no params)
    • Local time methods of Date : toLocaleString – returns a string of the date getDate – returns the day of the month getMonth – returns the month of the year (0 – 11) getDay – returns the day of the week (0 – 6) getFullYear – returns the year getTime – returns the number of milliseconds since January 1, 1970 getHours – returns the hour (0 – 23) getMinutes – returns the minutes (0 – 59) getMilliseconds – returns the millisecond (0 – 999)

4.5 Screen Output & Keyboard Input

  • The JavaScript model for the HTML document is the Document object
  • The model for the browser display window is the Window object
    • The Window object has two properties, document and window , which refer to the Document and Window objects, respectively
  • The Document object has a method, write , which dynamically creates content
    • The parameter is a string, often catenated from parts, some of which are variables e.g., document.write("Answer: " + result + "");
    • The parameter is sent to the browser, so it can be anything that can appear in an HTML document ( , but not \n )
  • The Window object has three methods for creating dialog boxes, alert , confirm , and prompt

4.6 Control Statements

  • Similar to C, Java, and C++
  • Compound statements are delimited by braces, but compound statements are not blocks
  • Control expressions – three kinds
    1. Primitive values
      • If it is a string, it is true unless it is empty or " 0 "
      • If it is a number, it is true unless it is zero
    2. Relational Expressions
      • The usual six : == , != , < , > , <= , >=
        • Operands are coerced if necessary
          • If one is a string and one is a number, it attempts to convert the string to a number
          • If one is Boolean and the other is not, the Boolean operand is coerced to a number (1 or 0)
      • The unusual two : === and !==
        • Same as == and != , except that no coercions are done (operands must be identical)

4.6 Control Statements (continued)

  1. Relational Expressions (continued)
    • Comparisons of references to objects are not useful (addresses are compared, not values)
  2. Compound Expressions
    • The usual operators: && , || , and!
      • The Boolean object has a method, toString , to allow them to be printed ( true or false )
  • Selection Statements
  • The usual if-then-else (clauses can be either single statements or compound statements)

4.6 Control Statements (continued)

  • Loop statements while ( control_expression ) statement or cmpnd for ( init ; control ; increment ) statement or cmpnd
  • init can have declarations, but the scope of such variables is the whole script  SHOW date.html do statement or compound while ( control_expression )

4.7 Object Creation and Modification

  • Objects can be created with new
  • The most basic object is one that uses the Object constructor, as in var myObject = new Object();
    • The new object has no properties - a blank object
    • Properties can be added to an object, any time

4.7 Object Creation and Modification

(continued) var myAirplane = new Object(); myAirplane.make = "Cessna"; myAirplane.model = "Centurian";

  • Objects can be nested, so a property could be itself another object, created with new
  • Properties can be accessed by dot notation or in array notation, as in var property1 = myAirplane["model"]; delete myAirplane.model;
  • Another Loop Statement
  • for ( identifier in object ) statement or compound for (var prop in myAirplane) document.write(myAirplane[prop] + "");

4.8 Arrays (continued)

  • Array methods:
    • join – e.g., var listStr = list.join(", ");
    • reverse
    • sort – e.g., names.sort();
      • Coerces elements to strings and puts them in alphabetical order
    • concat – e.g., newList = list.concat(47, 26);
    • slice listPart = list.slice(2, 5); listPart2 = list.slice(2);
    • toString
      • Coerce elements to strings, if necessary, and catenate them together, separated by commas (exactly like join(", ") )
    • push , pop , unshift , and shift  SHOW nested_arrays.html

4.9 Functions

  • function function_name ( [formal_parameters] ) { -- body – }
  • Return value is the parameter of return
    • If there is no return , or if the end of the function is reached, undefined is returned
    • If return has no parameter, undefined is returned
  • Functions are objects, so variables that reference them can be treated as other object references - If fun is the name of a function, ref_fun = fun; ... ref_fun(); / A call to fun /
  • We place all function definitions in the head of the the HTML document
  • All variables that are either implicitly declared or explicitly declared outside functions are global
  • Variables explicitly declared in a function are local