Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.

Similar presentations


Presentation on theme: "Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc."— Presentation transcript:

1 Basic Objects

2 Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.

3 Number Object  Wrapper for numbers  Number.MAX_VALUE  Number.MIN_VALUE  Number.NaN  Number.NEGATIVE_INFINITY  Number.POSITIVE_INFINITY

4 String Object  charAt( index )  indexOf( substring, start_index )  substr( start, length )  substring( start, end ) – end not included  slice( start, end ) - end not included  toLowerCase()  toUpperCase()

5 Type Coercion  “October “ + 2005  + considered to be concatenation, resulting in “October 2005”  2005 + “October”  2005 * “2” treated as 4010

6 Explicit Type conversions var num = 6; var str_value = String(num); var str_value = num.toString(); var str_value_binary = num.toString(radix); var str_value_binary = num.toString(2);

7 typeof  evaluates to “number” if primitive type “Number”  to “string”, or “boolean”  evaluates to “object” if operand is an object or “null”

8 Date Object  var today = new Date();  Methods getDate getMonth getDay getFullYear getTime ms since Jan 1, 1970 getHours0 to 23 getMinutes0 to 59 getSeconds0 to 59 getMilliseconds 0 to 999

9 Screen Output  document.write (“a string “)  document.writeln(“a string “) the string is often xhtml tags

10 Window  alert method opens a dialog window + OK button alert (parameter) parameter is plain text  confirm method opens a dialog window + OK + Cancel button  prompt (parameter) opens a dialog window with a textfield  see Sebesta_ch04\roots.html

11 Object Creation and Modification  Objects can be created with new  most basic: use the Object constructor, e.g.  var myObject = new Object(); no properties - a blank object Properties can be added to an object, any time

12 Adding Properties Dynamically var myCar = new Object(); myCar.make = “Toyota"; myCar.model = “Sienna"; - Objects can be nested, so a property could be itself another object, created with keyword new myCar.engine = new Object(); myCar.engine.config = “V6”; myCar.engine.hp = 210;

13 Accessing Properties - Properties can be accessed by dot notation  var property1 = myCar.make in array notation, as in  var property2 = myCar["model"]; - delete myCar.model;

14 Listing Properties of an object  for-in Loop Statement  - for (identifier in object) statement or compound statement  for (var prop in myCar)  document.write(myCar[prop] + " ");  See 6A-4createObject.html

15 Array Object Creation  Two ways: var my_arr = new Array ( 1, 2, “A”, “B”);// 4 var an_arr = new Array (50); //50 elements  Alternative var my_arr2 = new Array [ 1, 2, “A”, “B”];//

16 Array Properties  Lowest index is 0  length of array is dynamic var my_arr = new Array ( 1, 2, “A”, “B”);// 4 my_arr.length // length is 4 my_arr[17] = 1700; //not error! lengthens my_arr.length // length is 18 my_arr.length = 1000 // length is now 1000  Only assigned elements occupy space in heap  See example insert_names.html

17 Array Methods  Chapter 11, p. 362-402  join() – converts all elements of an array to strings and concatenates them into a single string array.join() – all values separated by comma array.join ( string-delimiter ); all values separated by string-delimiter  reverse()  sort(), p.378 – coerces elements to strings, if not already strings, and sort in alphabetical order  concat( parameter-list);  slice similar to substring of strings  toString similar to join  shift – removes an element at the beginning of array  unshift – adds an elements at the beginning of array  See example: medians.html

18 IE Javascript Debugging Aids  IE6 default: no debugging aid for Javascript  Change setting: Tools > Internet Options > Advanced tab uncheck the box “Disable script debugging” check the box “Display a notification about every script error” press “Apply” button  See 6A-8debug.html  http://www.microsoft.com/scripting/debugger/def ault.htm http://www.microsoft.com/scripting/debugger/def ault.htm

19

20 Error Message in previous IE versions

21 New IE Error Message

22 Netscape Javascript Debugger  Tools > Web Development > JavaScript Console  Keep the JavaScript Console open when using Netscape to display a document with Javascript  After debugging, use the clear button on the console. Otherwise the old error message remains and may cause confusion  Reference:  http://www.mozilla.org/projects/venkman / http://www.mozilla.org/projects/venkman /

23

24 Error Console of Firefox


Download ppt "Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc."

Similar presentations


Ads by Google