Docsity

JavaScript Fundamentals: An Overview, Lecture notes of Web Programming and Technologies

Toggle sidebar
bg1
Overview of JavaScript
➢Originally developed by Netscape
➢Joint Development with Sun Microsystems in 1995
➢Standard 262 (ECMA-262) of the European Computer Manufacturers Association
➢ECMA-262 edition 3 is the current standard
•Edition 4 is under development
➢Supported by Netscape, Mozilla, Internet Explorer
➢Differences
JavaScript has a different object model from Java
JavaScript is not strongly typed
➢Java 1.6 has support for scripting
http://java.sun.com/javase/6/docs/technotes/guides/scripting/index.html
➢Mozilla Rhino is an implementation of JavaScript in Java
http://www.mozilla.org/rhino/
Origins
Java and JavaScript
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43

Partial preview of the text

Download JavaScript Fundamentals: An Overview and more Lecture notes Web Programming and Technologies in PDF only on Docsity!

Overview of JavaScript

➢ Originally developed by Netscape ➢ Joint Development with Sun Microsystems in 1995 ➢ Standard 262 (ECMA-262) of the European Computer Manufacturers Association ➢ ECMA- 262 edition 3 is the current standard

  • Edition 4 is under development ➢ Supported by Netscape, Mozilla, Internet Explorer ➢Differences JavaScript has a different object model from Java JavaScript is not strongly typed ➢Java 1.6 has support for scripting http://java.sun.com/javase/6/docs/technotes/guides/scripting/index.html ➢Mozilla Rhino is an implementation of JavaScript in Java http://www.mozilla.org/rhino/ Origins Java and JavaScript

JavaScript Components

➢ Core

The heart of the language

➢ Client-side

Library of objects supporting browser control and user interaction

➢ Server-side

Library of objects that support use in web servers

➢ Text focuses on Client-side

XHTML/JavaScript Documents ➢ When JavaScript is embedded in an XHTML document , the browser interpret ‘s it with Java Interpretor to run or execute. ➢ Two locations for JavaScript server different purposes

  • JavaScript in the head element will react to user input and be called from other locations
  • JavaScript in the body element will be executed once as the page is loaded ➢ Various strategies must be used to ‘protect’ the JavaScript from the browser
  • For example, comparisons present a problem since < and > are used to mark tags in XHTML
  • JavaScript code can be enclosed in XHTML comments
  • JavaScript code can be enclosed in a CDATA section

JavaScript in XHTML: CDATA ➢ The **** block is intended to hold data that should not be interpreted as XHTML ➢ Using this should allow any data (including special symbols and -- ) to be included in the script ➢ This, however does not work, at least in Firefox: __ ➢ The problem seems to be that the CDATA tag causes an internal JavaScript error JavaScript in XHTML ➢ This does work in Firefox __ ➢ The comment symbols do not bother the XML parser (only /* and */ are ‘visible’ to it) ➢ The comment symbols protect the CDATA markers from the JavaScript parser

➢ Directly embedded < **_script type=“text/javascript”>

** ➢ However, note that a-- will not be allowed here! ➢ Indirect reference ** General Syntactic Characteristics ➢ Identifiers

  • Case sensitive ➢ Reserved words: alert, etc ➢ Keywords: break, case, fuction, typeof… etc ➢ Comments
  • //
  • /* … */ ➢ Statements can be terminated with a semicolon ➢ However, the interpreter will insert the semicolon if missing at the end of a line and the statement seems to be complete ➢ Can be a problem: ➢ return ➢ x; ➢ If a statement must be continued to a new line, make sure that the first line does not make a complete statement by itself

Primitive Types ➢ Five primitive types

  • Number
  • String
  • Boolean
  • Undefined
  • Null ➢ There are five classes corresponding to the five primitive types
  • Wrapper objects for primitive values
  • Place for methods and properties relevant to the primitive types
  • Primitive values are coerced to the wrapper class as necessary, and vice-versa

Primitive and Object Storage

Declaring Variables ➢ JavaScript is dynamically typed , that is, variables do not have declared types

  • A variable can hold different types of values at different times during program execution ➢ A variable is declared using the keyword var var counter, index, pi = 3.14159265, quarterback = "Elway", stop_flag = true; Numeric Operators ➢ Standard arithmetic
        • / % ➢ Increment and decrement
  • -- ++
  • Increment and decrement differ in effect when used before and after a variable
  • Assume that a has the value 3, initially
  • (++a) * 3 has the value 12
  • (a++) * 3 has the value 9
  • a has the final value 4 in either case

Precedence of Operators Operators Associativity ++, --, unary - (^) Right *, /, % (^) Left +, - (^) Left >, <, >= ,<= (^) Left ==, != (^) Left ===,!== (^) Left && (^) Left || (^) Left *=, +=, - =, =, /=, &&=, ||=, %= (^) Right Example of Precedence: var^ a = 2,b = 4,c,d; c = 3 + a * b; // * is first, so c is now 11 (not 24) d = b / a / 2; // / associates left, so d is now 1 (not 4) Special: = = = : 1 = = “1” returns true, but its not, so 1 === “1” returns false

String Catenation ➢ The operation + is the string catenation operation ➢ In many cases, other types are automatically converted to string Implicit Type Conversion ➢ JavaScript attempts to convert values in order to be able to perform operations ➢ “August “ + 1977 causes the number to be converted to string and a concatenation to be performed ➢ 7 * “3” causes the string to be converted to a number and a multiplication to be performed ➢ null is converted to 0 in a numeric context, undefined to NaN ➢ 0 is interpreted as a Boolean false , all other numbers are interpreted a true ➢ The empty string is interpreted as a Boolean false, all other strings (including “0”!) as Boolean true ➢ undefined, Nan and null are all interpreted as Boolean false

Explicit Type Conversion ➢ Explicit conversion of string to number

  • Number(aString)
  • aString – 0
  • Number must begin the string and be followed by space or end of string ➢ parseInt and parseFloat convert the beginning of a string but do not cause an error if a non-space follows the numeric part String Properties and Methods ➢ One property: length ➢ Character positions in strings begin at index 0

Example: Var str=“George” var len = str.length; // Str.charAt(2) //o str.indexof ( ‘ r ’ ) // Str.substring(2,4) //or Str.toLowerCase() /george

The typeof Operator ➢ Returns type of single operand ➢ Returns “number” or “string” or “boolean” for primitive types ➢ Returns “object” for an object or null ➢ Two syntactic forms

  • typeof x or typeof(x) Assignment Statements ➢ Plain assignment indicated by = ➢ Compound assignment with += - = /= *= %= … a += 7 means the same as a = a + 7 The Date Object ➢ A Date object represents a time stamp , that is, a point in time ➢ A Date object is created with the new operator
  • var now= new Date();
  • This creates a Date object for the time at which it was created