CIS525 - Lecture#7 - September 25, 2000

CIS 525 9/27/00 More on JavaScript: Objects in Javascript - created can be similar to objects in C++, with some differences this.firstname = firstname; => accessing and assigning An Object Example: <html><head> <title> An Object Example </title> <script language "JavaScript"> function person(firstname, lastname, age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } person1 = new person("logan", "blankenschplatz", "87"); Another way of creating/assigning objects: var test = new Object(); test.field1 = "value1"; Caution: there is no 'spellchecking' in JavaScript!! Any misspelling may result in an unwanted new field In JavaScript 1.2, you can assign values thusly: Object2 = (x:3, y:4, z:5) This is equivalent to: var Object2 = new Object(); Object2.x = 3; Object2.y = 4; Object2.z = 5; In JavaScript, "function" is a reserved word: function square(x) { return(x*x); } Recursion: function factorial(n) { if(n<=1) return(1); else return(n*factorial(n-1)); } For dynamic html: function printheading(message) { document.writeln("<H1>" + message + "</H1>"); } SEE SOURCE CODE IN CHAPTER 19 OF HALL "ShowInfo.html", (best viewed in NotePad), to see the JavaScript code for ShowInfo functionality In JavaScript, the attribute of an object can be a function return An example: this.printStats = printStats; function printStats() { document.write(this.firstname + " "); document.write("age=" + this.age + " "); } JavaScript has a parser utility: parseFloat, (or parseString, parseInt, etc), an example: parseFloat("137") =>returns 137 parseFloat("137abc") =>returns 137 parseFloat("abc137") =>returns 0, doesn't see a number, and breaks parseFloat("1abc37") =>returns 1, sees first number, then breaks Arrays in JavaScript: myArray = InitArray(10); myArray[1] = "South Carolina"; myArray[2] = "Oregon"; =>assigning values to indexes OR: var squares = nw Array(5); for(var i = 0; i < squares.length; i++) squares[i] = i*i; } OR: var squares = new Array(0, 1, 4, 9, 16); Different methods of assigning values to indexes: var ArrayObj = new Object(); ArrayObj[0] = "Index zero"; ArrayObj[10] = "Index ten"; =>nothing is placed in indices 1-9 ArrayObj.field1 = "field1"; =>index in this case is actually text, ('field') ArrayObj["field2"] = "field2"; =>index is text here as well SEE PAGE 1055 IN HALL FOR CODE "ForIn.html", AN EXAMPLE OF ITERATING OVER ARRAY INDEXES another example: for(field in object) =>'field' in this case is the index | | object[field]; =>returns value of 'field' index of object array Event driven programming: JavaScript is used for many evnet driven applications SEE CHAP 19 OF HALL FOR CODE "DontClick.html", (an event program in a form) Other event functions: OnChange =>for any change in a specific field/document OnClick =>for a mouse click OnBlur =>when focus is removed from an object/input area OnFocus OnLoad =>when a page/doc loads OnMouseOver OnSelect =>radio buttons or check boxes OnSubmit =>get/post data, etc OnUnLoad =>unload page or document THE LINK BELOW WILL TAKE YOU TO A WEB PAGE ON JAVASCRIPT WITH NUMEROUS EXAMPLES Javascript Information (CIS 400 Programming Languages Guide)