25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.

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

Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
JavaScript Language Fundamentals. About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript”
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
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.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
18-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Tutorial 10 Programming with JavaScript
24-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
JavaScript, Third Edition
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.
12-Aug-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Java Unit 9: Arrays Declaring and Processing Arrays.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Lists in Python.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Using Client-Side Scripts to Enhance Web Applications 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
27-Oct-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Built-in Data Structures in Python An Introduction.
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.
Functions and Arrays. Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and.
15-Nov-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
Unit 12 JavaScript Arrays Instructor: Brent Presley.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Overview JavaScript. Overview 2 About Basics Objects DOM.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript” –The name was changed when Java became.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
6-Jun-16 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
SEEM4570 Tutorial 05: JavaScript as OOP
CIT 383: Administrative Scripting
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
Homework Any Questions?.
Tutorial 10 Programming with JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
CIS 136 Building Mobile Apps
Lisp.
Presentation transcript:

25-Jun-15 JavaScript Language Fundamentals II

2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression creates and throws an exception The expression is the value of the exception, and can be of any type (often, it's a literal String) try { statements to try } catch ( e ) { // Notice: no type declaration for e exception handling statements } finally { // optional, as usual code that is always executed } With this form, there is only one catch clause

3 Exception handling, II try { statements to try } catch ( e if test1 ) { exception handling for the case that test1 is true } catch ( e if test2 ) { exception handling for when test1 is false and test2 is true } catch ( e ) { exception handling for when both test1and test2 are false } finally { // optional, as usual code that is always executed } Typically, the test would be something like e == "InvalidNameException"

4 Array literals You don’t declare the types of variables in JavaScript JavaScript has array literals, written with brackets and commas Example: color = ["red", "yellow", "green", "blue"]; Arrays are zero-based: color[0] is "red" If you put two commas in a row, the array has an “empty” element in that location Example: color = ["red",,, "green", "blue"]; color has 5 elements However, a single comma at the end is ignored Example: color = ["red",,, "green", "blue”,]; still has 5 elements

5 Four ways to create an array You can use an array literal: var colors = ["red", "green", "blue"]; You can use new Array() to create an empty array: var colors = new Array(); You can add elements to the array later: colors[0] = "red"; colors[2] = "blue"; colors[1]="green"; You can use new Array( n ) with a single numeric argument to create an array of that size var colors = new Array(3); You can use new Array(…) with two or more arguments to create an array containing those values: var colors = new Array("red","green", "blue");

6 The length of an array If myArray is an array, its length is given by myArray.length Array length can be changed by assignment beyond the current length Example: var myArray = new Array(5); myArray[10] = 3; Arrays are sparse, that is, space is only allocated for elements that have been assigned a value Example: myArray[50000] = 3; is perfectly OK But indices must be between 0 and As in C and Java, there are no two-dimensional arrays; but you can have an array of arrays: myArray[5][3]

7 Arrays and objects Arrays are objects car = { myCar: "Saturn", 7: "Mazda" } car[7] is the same as car.7 car.myCar is the same as car["myCar"] If you know the name of a property, you can use dot notation: car.myCar If you don’t know the name of a property, but you have it in a variable (or can compute it), you must use array notation: car["my" + "Car"]

8 Array functions If myArray is an array, myArray.sort() sorts the array alphabetically myArray.sort(function(a, b) { return a - b; }) sorts numerically myArray.reverse() reverses the array elements myArray.push(…) adds any number of new elements to the end of the array, and increases the array’s length myArray.pop() removes and returns the last element of the array, and decrements the array’s length myArray.toString() returns a string containing the values of the array elements, separated by commas

9 The for…in loop You can loop through the properties of an object with for ( variable in object ) statement ; Example: for (var prop in course) { document.write(prop + ": " + course[prop]); } Possible output: teacher: Dr. Dave number: CIT597 The properties are accessed in an undefined order If you add or delete properties of the object within the loop, it is undefined whether the loop will visit those properties Arrays are objects; applied to an array, for…in will visit the “properties” 0, 1, 2, … Notice that course["teacher"] is equivalent to course.teacher You must use brackets if the property name is in a variable

10 More about the for...in loop The for...in loop does not loop through all properties of an object Built-in methods, and many built-in properties, are flagged as nonenumerable All built-in properties of functions are nonenumerable There are lots of little surprises like this in JavaScript 

11 The with statement with ( object ) statement ; uses the object as the default prefix for variables in the statement For example, the following are equivalent: with (document.myForm) { result.value = compute(myInput.value) ; } document.myForm.result.value = compute(document.myForm.myInput.value); Some books recommend against the use of with, because it can be confusing

12 Regular expressions A regular expression can be written in either of two ways: Within slashes, such as re = /ab+c/ With a constructor, such as re = new RegExp("ab+c") Regular expressions are almost the same as in Perl or Java (only a few unusual features are missing) string.match( regexp ) searches string for an occurrence of regexp It returns null if nothing is found If regexp has the g (global search) flag set, match returns an array of matched substrings If g is not set, match returns an array whose 0 th element is the matched text, extra elements are the parenthesized subexpressions, and the index property is the start position of the matched substring

13 Warnings JavaScript is a big, complex language We’ve only scratched the surface It’s easy to get started in JavaScript, but if you need to use it heavily, plan to invest time in learning it well Write and test your programs a little bit at a time JavaScript is not totally platform independent Expect different browsers to behave differently Write and test your programs a little bit at a time Browsers aren’t designed to report errors Don’t expect to get any helpful error messages Write and test your programs a little bit at a time

14 Evaluation (i.e., Dave’s opinion) JavaScript, like so many other languages, borrows most of its syntax from C JavaScript is a scripting language It has lots of convenience features Global variables Not having to declare variables at all Untyped variables Easy modification of objects JavaScript is not designed for large programs My experience is that JavaScript is very nice if you use it for the purposes that its designers expected, but very ugly if you try to use it in non-routine ways

15 The End