Download presentation
Presentation is loading. Please wait.
Published byBartholomew York Modified over 9 years ago
1
Chapter 12: Objects CIS 275—Web Application Development for Business I
2
2 Thinking About Objects JavaScript is an object-_______ programming language (Math, Array, document, window, …). All objects have attributes and exhibit __________. Objects ____________ or enclose their attributes and behaviors. Programs communicate with objects through __________ (basically function calls). Objects possess the property of information ______. We don’t know (or care) how the object does things. We just care about what it does. Using existing objects makes programming easier. based behaviors encapsulate interfaces hiding
3
3 Math Object Rounding a number Rounding document.writeln(Math.round(7.25)) displays _____ The round method of the Math class has the argument 7.25. ________ is a method of the document object. Math.PI is approximately _______________ Find the minimum of a series of numbersminimum Math.min(2, 4, 6, 7, 1) returns _____ Find the square root of a number Math.sqrt(49) returns ____ Raise a number to a power Math.pow(2, 8) returns _____ 7 1 7 256 3.14159265 writeln
4
4 The String Object The String Object I JavaScript supports the set of characters called _________. For a string object, the length property returns the number of characters in the string. If lastname=“Johnson”, then lastname.length = ____ You can change the color of text using the fontcolor() method of the string object. document.write(" " + lastname.fontcolor(‘red’) + " ") The indexOf() method returns the starting position of text within a string object, beginning with 0. lastname.indexOf(“son”) = _____ 7 4 Unicode
5
5 The String Object The String Object II The match() method of the string object returns a substring if it exists. lastname.match(“son”) = _______ The substring(x, y) method returns a substring starting at position x for y characters. lastname.substring(0, 4) = ______ The toLowerCase() and toUpperCase() methods convert the characters in a string object to lower or upper case. lastname.toUpperCase() = _____________ See pp. 384-385 for more String methods. son John JOHNSON
6
6 Fig. 12.4 CharacterProcessing.html Character Processing Methods <!-- var s = "ZEBRA"; var s2 = "AbCdEfG"; document.writeln( " Character at index 0 in ‘ " + s + “ ' is " + s.charAt( 0 ) ); document.writeln( " Character code at index 0 in '" + s + "' is " + s.charCodeAt( 0 ) + " " ); document.writeln( " '" + String.fromCharCode( 87, 79, 82, 68 ) + "' contains character codes 87, 79, 82 and 68 " ) document.writeln( " '" + s2 + "' in lowercase is '" + s2.toLowerCase() + "'" ); document.writeln( " '" + s2 + "' in uppercase is '" + s2.toUpperCase() + "' "); // -->
7
7 Date ObjectDate Object I Create a date object (assume the date is 7/10/2002) var today = new Date() Methods for the date object today.getMonth() returns _____ today.getDate() returns _____ today.getFullYear() returns _______ You can also use getHours(), getMinutes, getSeconds() Check the interesting examples of displaying day, date, and time.day datetime 7 10 2004
8
8 Date ObjectDate Object II You can create a date object and set the date: var aDate = new Date("October 12, 1988 13:14:00") var aDate = new Date("October 12, 1988") var aDate = new Date(88,09,12,13,14,00) // Oct 12, 1:14 p var aDate = new Date(88,09,12) // Oct 12, 1988 var aDate = new Date(500) // in milliseconds You can reset parts of a date as follows: var today = new Date() today.setDate(today.getDate()-1) document.write(today.getDate() + " ")
9
9 Window ObjectWindow Object I Alert box Alert alert("Hello World!") Confirm box Confirm var name = confirm("Press a button") if (name == true) { document.write("You pressed OK") } else { document.write("You pressed Cancel") }
10
10 Window ObjectWindow Object II Prompt box Prompt var name = prompt("Please enter your name",“Right here") if (name != null && name != "") { document.write("Hello " + name) } Function to open two new windowsopen function openwindow() { window.open("http://www.microsoft.com") window.open("http://www.w3schools.com") } Sending to a new locationlocation location="http://www.w3schools.com/"
11
11 Window ObjectWindow Object III Refresh a document Refresh location.reload() Write text to the status barstatus window.status = "put your message here" Print a page Print window.print()
12
12 Frame Object Breaking out of a _________ Breaking out if (window.top != window.self) { window.top.location=“apage.htm" } Updating framesframes parent.upperframe.location.href="frame_b.htm" frame
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.