COMP3421_Lecture_05.pdf - COMP3421 Web Application Design and Development Lecture 5 More on JavaScript Agenda \u203a JavaScript Objects \u2013 Math String

COMP3421_Lecture_05.pdf - COMP3421 Web Application Design...

This preview shows page 1 - 9 out of 24 pages.

COMP3421 Web Application Design and Development Lecture 5More on JavaScript
Image of page 1
Agenda JavaScript Objects Math, String, Date and Number objects DOM objects Event Handlers Inline Model Traditional Model 1
Image of page 2
JavaScript Objects JavaScript supports object-based programming Object Resides in the computer’s memory and contains information used by the script The term object normally implies that attributes (data) and behaviors (methods) are associated with the object An object’s methods use the attributes’ data to perform useful actions for the client of the object the script that calls the methods 2
Image of page 3
Example The Math Object Math object ( ) methods allow you to perform many common mathematical calculations › An object’s methods are called by writing the name of the object followed by a dot operator (.) and the name of the method In parentheses following the method name is the argument (or a comma-separated list of arguments) to the method 3
Image of page 4
Example The String Object A string is a series of characters treated as a single unit A string may include letters, digits and various special characters, such as +, -, *, /, and $ JavaScript supports Unicode, which represents a large portion of the world’s languages String literals or string constants (often called anonymous String objects) are written as a sequence of characters in double quotation marks or single quotation marks 4
Image of page 5
5 <html> <head> <title>Character Processing Methods</title> </head> <body> <script type="text/javascript"> var s = "ZEBRA"; var s2 = "AbCdEfG"; document.writeln("<p>Character at index 0 in '" + s + "' is + " + s.charAt(0)); document.writeln("<br />Character code at index 0 in '" + s + "' is " + s.charCodeAt(0) + "</p>"); document.writeln("<p>'" + String.fromCharCode(87, 79, 82, 68) + "' contains character codes 87, 79, 82 and 68</p>"); document.writeln("<p>'" + s2 + "' in lowercase is '" + s2.toLowerCase() + "'"); document.writeln("<p>'" + s2 + "' in uppercase is '" + s2.toUpperCase() + "'"); </script> </body> </html> string.html
Image of page 6
The String Object String method indexOf Determines the location of the first occurrence of its argument in the string used to call the method If the substring is found, the index at which the first occurrence of the substring begins is returned; otherwise, -1 is returned Receives an optional second argument specifying the index from which to begin the search String method lastIndexOf Determines the location of the last occurrence of its argument in the string used to call the method If the substring is found, the index at which the last occurrence of the substring begins is returned; otherwise, -1 is returned Receives an optional second argument specifying the index from which to begin the search 6
Image of page 7
<html> <head> <title>String Methods</title> <script type="text/javascript"> var letters = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"; function buttonPressed() { var inputField = document.getElementById("inputField");
Image of page 8
Image of page 9

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture