Скачать презентацию Java Script Language Fundamentals II Exception handling Скачать презентацию Java Script Language Fundamentals II Exception handling

3-intermediate-javascript.ppt

  • Количество слайдов: 15

Java. Script Language Fundamentals II Java. Script Language Fundamentals II

Exception handling, I n n Exception handling in Java. Script is almost the same Exception handling, I n n Exception handling in Java. Script is almost the same as in Java throw expression creates and throws an exception n n 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 } n With this form, there is only one catch clause 2

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

Array literals n n You don’t declare the types of variables in Java. Script Array literals n n You don’t declare the types of variables in Java. Script has array literals, written with brackets and commas n n n 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 n Example: color = ["red", , , "green", "blue"]; n n color has 5 elements However, a single comma at the end is ignored n Example: color = ["red", , , "green", "blue”, ]; still has 5 elements 4

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

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

Arrays and objects n n Arrays are objects car = { my. Car: Arrays and objects n n Arrays are objects car = { my. Car: "Saturn", 7: "Mazda" } n n car[7] is the same as car. 7 car. my. Car is the same as car["my. Car"] If you know the name of a property, you can use dot notation: car. my. Car 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"] 7

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

The for…in loop n You can loop through the properties of an object with The for…in loop n You can loop through the properties of an object with for (variable in object) statement; n n n Example: for (var prop in course) { document. write(prop + ": " + course[prop]); } Possible output: teacher: Dr. Dave number: CIT 597 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 n You must use brackets if the property name is in a variable 9

More about the for. . . in loop n The for. . . in More about the for. . . in loop n The for. . . in loop does not loop through all properties of an object n n n 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 Java. Script 10

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

Regular expressions n A regular expression can be written in either of two ways: Regular expressions n A regular expression can be written in either of two ways: n n Within slashes, such as re = /ab+c/ With a constructor, such as re = new Reg. Exp("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 n n n 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 12

Warnings n Java. Script is a big, complex language n n Java. Script is Warnings n Java. Script is a big, complex language n n Java. Script is not totally platform independent n n n We’ve only scratched the surface It’s easy to get started in Java. Script, 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 Expect different browsers to behave differently Write and test your programs a little bit at a time Browsers aren’t designed to report errors n n Don’t expect to get any helpful error messages Write and test your programs a little bit at a time 13

Evaluation (i. e. , Dave’s opinion) n n Java. Script, like so many other Evaluation (i. e. , Dave’s opinion) n n Java. Script, like so many other languages, borrows most of its syntax from C Java. Script is a scripting language n It has lots of convenience features n n n Global variables Not having to declare variables at all Untyped variables Easy modification of objects Java. Script is not designed for large programs My experience is that Java. Script 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 14

The End 15 The End 15