JS1-1 Introduction to JavaScript (JavaScript 2) Xingquan (Hill) Zhu

Slides:



Advertisements
Similar presentations
COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)
Advertisements

The Web Warrior Guide to Web Design Technologies
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Tutorial 10 Programming with JavaScript
Chapter 6 © 2002 by Addison Wesley Longman, Inc. 1 Chapter 6 Sebesta: Programming the World Wide Web.
Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Exam 1 Corrected –3 A’s, 3 B’s and 2 C’s I have looked at all the assignment.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Questions? Assignment 1 due January 31 (TUESDAY) Discussions on JAVASCRIPT.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Debugging JavaScript CS346. IE Javascript Debugging Aids From IE6 on default: no debugging aid for Javascript Change setting: – Tools > Internet Options.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Chapter 4 © 2003 by Addison-Wesley, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
Chapter 4 The Basics of JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Overview of JavaScript - Originally developed by Netscape,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Using Client-Side Scripts to Enhance Web Applications 1.
Chapter 4 © 2010 by Addison Wesley Longman, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
1 Introduction to Web Application Introduction to Java Script.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Overview of JavaScript Developed by Netscape, as LiveScript Became a joint venture of Netscape and Sun in 1995, renamed JavaScript Now standard of the.
JavaScript, Fourth Edition
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
Chapter 4 © 2014 by Pearson Education Overview of JavaScript - Originally developed by Netscape by Brendan Eich, as LiveScript - Became a joint venture.
1 JavaScript in Context. Server-Side Programming.
COP 3813 Intro to Internet Computing Prof. Roy Levow Lecture 4 JavaScript.
Chapter 4 © 2013 by Pearson Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape and Sun.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
JavaScript and Ajax Week 10 Web site:
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
4.1 Overview of JavaScript
4.1 Overview of JavaScript
Tutorial 10 Programming with JavaScript
Chapter 4 Client-Side Programming: the JavaScript Language
Scope, Objects, Strings, Numbers
JavaScript Fundamentals
JavaScript: Functions.
ARRAYS MIT-AITI Copyright 2001.
WEB PROGRAMMING JavaScript.
The Basics of JavaScript
T. Jumana Abu Shmais – AOU - Riyadh
CS5220 Advanced Topics in Web Programming JavaScript Basics
Tutorial 10 Programming with JavaScript
CS3220 Web and Internet Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
4.1 Overview of JavaScript
JavaScript: Introduction to Scripting
CS3220 Web and Internet Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

JS1-1 Introduction to JavaScript (JavaScript 2) Xingquan (Hill) Zhu

JS1-2 JS  Object  Array  Function  JavaScript Object  Constructor  Widow object  Document object  JS Debugging

JS1-3 Object  Objects can be created with new  Math, Date, String, Number,  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 var customerOrder = new Object(); customerOrder.discount=false; customerOrder.quantity = 4; customerOrder.payment =“Visa"; Examples

JS1-4 Object  Objects can be nested, so a property could be itself another object, created with new var customerOrder = new Object(); customerOrder.customer=new Object(); customerOrder.discount=false; customerOrder.quantity = 4; customerOrder.payment = “Visa"; customerOrder.customer.name=“John”; customerOrder.customer.address=“777 Glades Road”;

JS1-5 Object  Properties can be accessed by dot notation or in array notation, as in  var thePayment=customerOrder.payment;  var thePayment=customerOrder[“payment"];ExampleExample  delete customerOrder.payment;  Another Loop Statement to access members in the Object  for (identifier in object) { statement or compound statement }  for (var prop in customerOrder) { document.write(customerOrder[prop] + " ");} Example

JS1-6 Array  Objects with some special functionality  Array objects can be created in two ways, with new, or by assigning an array literal  var myList = new Array(24, "bread", true);  var myList2 = [24, "bread", true];  var myList3 = new Array(24);  Multi dimensional arrayExampleExample  Var myList=[[24, “bread”,true],[true,”milk”]]  Array elements can be primitive values or references to other objects  Access array elementsExampleExample  myList[0], myList[1]….myList[myList.length-1];  the length property stores the length  The length of an array is the highest subscript to which an element has been assigned, plus 1  myList[122] = "bitsy"; // length is 123  Length is dynamic

JS1-7 Array Methods  Join  e.g., var listStr = list.join(", ");ExampleExample  Reverse  Sort  Coerces elements to strings and puts them in alphabetical order  e.g., names.sort();ExampleExample  Concat  e.g., newList = list.concat(47, 26);ExampleExample  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 web/cgi/javamanual/javaarray.html web/cgi/javamanual/javaarray.html

JS1-8 Functions  Why should we use functions  Divide-and-conquer  Software reusability  Packaged functions belonging to JS objects (Number.toString()) are called methods  Methods / functions interchangeable  Definition of a function  function function_name([formal_parameters]) ExampleExample { -- 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  A function is invoked by a function call  var iReturn=function_name([actual_parameters]);  actual_parameters: constants, variables, or expressions

JS1-9 Functions  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 */  Ensure that the interpreter sees the definition of a function before it sees a call to the function  We place all function definitions in the head of the the HTML document

JS1-10 Functions  All variables that are either implicitly declared or explicitly declared outside functions are global  Variables explicitly declared in a function are local Example  There is no type checking of parameters, nor is the number of parameters checked (excess actual parameters are ignored, excess formal parameters are set to undefined)ExampleExample  All parameters are sent through a property array, arguments, which has the length property

JS1-11 Functions  There is no clean way to send a primitive by reference  One dirty way is to put the value in an array and send the array’s name function by10(a) { a[0] *= 10; }... var listx = new Array(1);... listx[0] = x; by10(listx); x = listx[0];ExampleExample

JS1-12 Functions  To sort something other than strings into alphabetical order, write a function that performs the comparison and send it to the sort method  The comparison function must return a negative number, zero, or a positive number to indicate whether the order is ok, equal, or not function num_order(a, b) {return a - b;}  Now, we can sort an array named num_list with: num_list.sort(num_order) Example

JS1-13 JS  Object  Array  Function  JavaScript Object  Constructor  Widow object  Document object  JS Debugging

JS1-14 JS Objects  Constructor  Used to initialize objects, but actually create the properties  function customerOrder(bDiscount, iQuantity, sPayment) { this.discount = bDiscount; this.quantity = iQuantity; this.payment = sPayment; }  myOrder = new customerOrder(false, 3, “visa"); const.html

JS1-15 JS Objects  Can also have method properties  function displayOrder() { document.write(“Discount: ", this.discount, " "); document.write(“Quantity: ", this.quantity, " "); document.write(“Payment: ", this.payment, " "); }  Now add the following to the constructor  this.display = displayOrder; const_method.html

JS1-16 JS Objects  JavaScript Form Access const_method_radio.html

JS1-17 Document Object  Manipulate document that is currently visible in the browser window

JS1-18 Window Object  Provides methods for manipulating browser window

JS1-19 Window Object  simplewindow.html simplewindow.html  Timeout.htmlAnimation.html Timeout.htmlAnimation.html  window.html window.html cgi/javamanual/javawindow.html

JS1-20 Debugging JS  IE6  Select Internet Options from the Tools menu  Choose the Advanced tab  Uncheck the Disable script debugging box  Check the Display a notification about every  script error box  Now, a script error causes a small window to be  opened with an explanation of the error  NS7  Select Tasks, Tools, and JavaScript Console  A small window appears to display script errors  Remember to Clear the console after using an  error message – avoids confusion  Mozilla Firefox VenkMan JS Debugging ToolDebugJS.html  Old School Tool

JS1-21 JS  Object  Array  Function  JavaScript Object  Constructor  Widow object  Document object  JS Debugging