Slide 1 Introduction to JavaScript. www.BZUpages.com slide 2 Common Uses of JavaScript uForm validation uPage embellishments and special effects uNavigation.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

the javascript language BY SA.
Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
Programming Languages and Paradigms The C Programming Language.
Java Script Session1 INTRODUCTION.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
The Web Warrior Guide to Web Design Technologies
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Computer Science 103 Chapter 4 Advanced JavaScript.
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
JavaScript John Mitchell Adapted by Mooly Sagiv Reading: links on last slide Homework 1: 18/3 – 17/4 Spring 2012.
INF 212 ANALYSIS OF PROG. LANGS JAVASCRIPT Instructors: Crista Lopes Copyright © Instructors.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
JavaScript JavaScript is a scripting language that is most commonly used to add client- side programming to a web page. Some of the things it is used for.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Slide 1 Vitaly Shmatikov CS 345 Introduction to JavaScript.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Week 9 PHP Cookies and Session Introduction to JavaScript.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
JavaScript Functions A function is a block of code that can be reused. It is similar to a method in Java. It consists of a function name, an argument.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
Tutorial 11 1 JavaScript Operators and Expressions.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
JQUERY AND AJAX
JavaScript John Mitchell CS 242 Reading: links on last slide Homework 1: Sept 24 - Oct 1 Autumn 2008.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript John Mitchell Stanford University Revised by
JavaScript: Functions
Scope, Objects, Strings, Numbers
JavaScript Functions.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
Client/Server Computing and Web Technologies
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
CS5220 Advanced Topics in Web Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript Intro.
CIS 136 Building Mobile Apps
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

slide 1 Introduction to JavaScript

slide 2 Common Uses of JavaScript uForm validation uPage embellishments and special effects uNavigation systems uBasic math calculations uDynamic content manipulation uSample applications Dashboard widgets in Mac OS X, Google Maps, Philips universal remotes, Writely word processor, hundreds of others…

slide 3 Example 1: Add Two Numbers … var num1, num2, sum num1 = prompt("Enter first number") num2 = prompt("Enter second number") sum = parseInt(num1) + parseInt(num2) alert("Sum = " + sum) …

slide 4 Example 2: Browser Events function whichButton(event) { if (event.button==1) { alert("You clicked the left mouse button!") } else { alert("You clicked the right mouse button!") }} … … Mouse event causes page-defined function to be called Other events: onLoad, onMouseMove, onKeyPress, onUnLoad

slide 5 Example 3: Page Manipulation uSome possibilities createElement(elementName) createTextNode(text) appendChild(newChild) removeChild(node) uExample: add a new list item var list = document.getElementById('t1') var newitem = document.createElement('li') var newtext = document.createTextNode(text) list.appendChild(newitem) newitem.appendChild(newtext) This uses the browser Document Object Model (DOM). We will focus on JavaScript as a language, not its use in the browser

slide 6 Language Basics uJavaScript is case sensitive onClick, ONCLICK, … are HTML, thus not case-sensitive uStatements terminated by returns or semi-colons x = x+1; same as x = x+1 u“Blocks” of statements enclosed in { …} uVariables Define using the var statement Define implicitly by its first use, which must be an assignment –Implicit defn has global scope, even if occurs in nested scope!

slide 7 JavaScript Blocks uUse { } for grouping; not a separate scope js> var x=3; js> x 3 js> {var x=4; x} 4 js> x 4 uNot blocks in the sense of other languages

slide 8 JavaScript Primitive Datatypes uBoolean: true and false uNumber: 64-bit floating point Similar to Java double and Double No integer type Special values NaN (not a number) and Infinity uString: sequence of zero or more Unicode chars No separate character type (just strings of length 1) Literal strings using ' or " characters (must match) uSpecial objects: null and undefined

slide 9 Objects uAn object is a collection of named properties uThink of it as an associative array or hash table Set of name:value pairs –objBob = {name: “Bob", grade: 'A', level: 3}; Play a role similar to lists in Lisp / Scheme uNew members can be added at any time –objBob.fullname = 'Robert'; uCan have methods uCan refer to this

slide 10 Functions uFunctions are objects with method called “( )” A property of an object may be a function (=method) –function max(x,y) { if (x>y) return x; else return y;}; –max.description = “return the maximum of two arguments”; Local declarations may appear in function body uCall can supply any number of arguments functionname.length : # of arguments in definition functionname.arguments.length : # arguments in call Basic types are passed by value, objects by reference u“ Anonymous” functions (function (x,y) {return x+y}) (2,3);

slide 11 Examples of Functions uCurried functions –function CurriedAdd(x) { return function(y){ return x+y} }; –g = CurriedAdd(2); –g(3) uVariable number of arguments –function sumAll() { var total=0; for (var i=0; i< sumAll.arguments.length; i++) total+=sumAll.arguments[i]; return(total); } –sumAll(3,5,3,5,3,2,6)

slide 12 Anonymous Functions uAnonymous functions very useful for callbacks setTimeout(function() { alert("done"); }, 10000) Evaluation of alert("done") delayed until function call uSimulate blocks by function definition and call var u = { a:1, b:2 } var v = { a:3, b:4 } (function (x,y) { var tempA = x.a; var tempB =x.b; // local variables x.a=y.a; x.b=y.b; y.a=tempA; y.b-tempB }) (u,v) // Works because objs are passed by ref

slide 13 Basic Object Features uUse a function to construct an object function car(make, model, year) { this.make = make; this.model = model; this.year = year; } uObjects have prototypes, can be changed var c = new car(“Ford”,”Taurus”,1988); car.prototype.print = function () { return this.year + “ “ + this.make + “ “ + this.model;} c.print();

slide 14 JavaScript in Web Pages uEmbedded in HTML page as element JavaScript written directly inside element – alert("Hello World!") Linked file as src attribute of the element uEvent handler attribute uPseudo-URL referenced by a link Click me We are looking at JavaScript as a language; ignore BOM, DOM, AJAX

slide 15 Language Features in This Class uStack memory management Parameters, local variables in activation records uGarbage collection uClosures Function together with environment (global variables) uExceptions uObject features Dynamic lookup, encapsulation, subtyping, inheritance uConcurrency

slide 16 Stack Memory Management uLocal variables in activation record of function function f(x) { var y = 3; function g(z) { return y+z;}; return g(x); } var x= 1; var y =2; f(x) + y;

slide 17 Garbage Collection uAutomatic reclamation of unused memory uNavigator 2: per-page memory management Reclaim memory when browser changes page uNavigator 3: reference counting Each memory region has associated count Count modified when pointers are changed Reclaim memory when count reaches zero uNavigator 4: mark-and-sweep, or equivalent Garbage collector marks reachable memory Sweep and reclaim unreachable memory

slide 18 Closures uReturn a function from function call function f(x) { var y = x; return function (z){y += z; return y;} } var h = f(5); h(3); uCan use this idea to define objects with “private” fields (subtle) See

slide 19 Exceptions uThrow an expression of any type throw "Error2"; throw 42; throw {toString: function() { return "I'm an object!"; } }; uCatch try { } catch (e if e == “FirstException") { // do something } catch (e if e == “SecondException") { // do something else } catch (e){ // executed if no match above } Reference: Core_JavaScript_1.5_Guide :Exception_Handling_Statements

slide 20 Object features uDynamic lookup Method depends on run-time value of object uEncapsulation Object contains private data, public operations uSubtyping Object of one type can be used in place of another uInheritance Use implementation of one kind of object to implement another kind of object

slide 21 Concurrency uJavaScript itself is single-threaded How can we tell if a language provides concurrency? uAJAX provides a form of concurrency Create XMLHttpRequest object, set callback function Call request method, which continues asynchronously Reply from remote site executes callback function –Event waits in event queue… Closures important for proper execution of callbacks uAnother form of concurrency Use SetTimeout to do cooperative multi-tasking

slide 22 JavaScript eval uEvaluate string as code (seen this before?) The eval function evaluates a string of JavaScript code, in scope of the calling code –var code = "var a = 1"; –eval(code); // a is now '1‘ –var obj = new Object(); –obj.eval(code); // obj.a is now 1 Common use: efficiently deserialize a complicated data structure received over network via XMLHttpRequest uWhat does it cost to have eval in the language? Can you do this in C? What would it take to implement?

slide 23 Unusual Features of JavaScript uEval, run-time type checking functions uSupport for pattern matching (reg. expressions) uCan add methods to an object uCan delete methods of an object myobj.a = 5; myobj.b = 12; delete myobj.a; uIterate over methods of an object for (variable in object) { statements } uWith statement (“considered harmful” – why?) with (object) { statements }