This document discusses JavaScript objects and strings. It provides examples of using object methods and properties to perform tasks like mathematical calculations. It also covers string methods for manipulating and extracting information from strings, including concatenation, character extraction, case conversion, and substring extraction. The document also discusses the Date object and methods for working with dates and times.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
25 views72 pages
Javascript Objects
This document discusses JavaScript objects and strings. It provides examples of using object methods and properties to perform tasks like mathematical calculations. It also covers string methods for manipulating and extracting information from strings, including concatenation, character extraction, case conversion, and substring extraction. The document also discusses the Date object and methods for working with dates and times.
Rights Reserved. 5 } Math object methods enable you to conveniently 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 are arguments to the method
Rights Reserved. 16 } String method fromCharCode § Returns a string created from a series of Unicode values } String method toLowerCase § Returns the lowercase version of a string } String method toUpperCase § Returns the uppercase version of a string
Rights Reserved. 32 } Date object provides methods for date and time manipulations § Based either on the computer’s local time zone or on World Time Standard’s Coordinated Universal Time (abbreviated UTC) } Most methods have a local time zone and a UTC version } Empty parentheses after an object name indicate a call to the object’s constructor with no arguments § A constructor is an initializer method for an object § Called automatically when an object is allocated with new § The Date constructor with no arguments initializes the Date object with the local computer’s current date and time § A new Date object can be initialized by passing the number of milliseconds since midnight, January 1, 1970, to the Date constructor § Can also create a new Date object by supplying arguments to the Date constructor for year, month, date, hours, minutes, seconds and milliseconds. Hours, minutes, seconds and milliseconds arguments are all optional If any one of these arguments is not specified, a zero is supplied in its place If an argument is specified, all arguments to its left must be specified
Rights Reserved. 51 Problems with Cookies }They’re extremely limited in size. }Cookies cannot store entire documents. }If the user browses the same site from multiple tabs, all of the site’s cookies are shared by the pages in each tab. § This could be problematic in web applications that allow the user to purchase items.
Rights Reserved. 52 Introducing localStorage and sessionStorage }As of HTML5, there are two new mechanisms for storing key/ value pairs that help eliminate some of the problems with cookies. § Web applications can use the window object’s localStorage property to store up to several megabytes of key/value-pair string data on the user’s computer and can access that data across browsing sessions and browser tabs. § Web applications that need access to data for only a browsing session and that must keep that data separate among multiple tabs can use the window object’s sessionStorage property. There’s a separate sessionStorage object for every browsing session, including separate tabs that are accessing the same website.
Rights Reserved. 53 Favorite Twitter Searches App Using localStorage and sessionStorage }This app allows users to save their favorite (possibly lengthy) Twitter search strings with easy-to-remember, user-chosen, short tag names. Users can then conveniently follow the tweets on their favorite topics by visiting this web page and clicking the link for a saved search. }The user’s favorite searches are saved using localStorage, so they’re immediately available each time the user browses the app’s web page. }The app uses sessionStorage to determine whether the user has visited the page previously during the current browsing session. If not, the app displays a welcome message.
Rights Reserved. 64 } The localStorage object’s length represents the number of key/value pairs stored. } Method key receives an index as an argument and returns the corresponding key. } For simplicity, we use the onclick attributes of the dynamically generated Edit and Delete buttons to set the buttons’ event handlers—this is an older mechanism for registering event handlers. } To register these with the elements’ addEventListener method, we’d have to dynamically locate the buttons in the page after we’ve created them, then register the event handlers, which would require significant additional code. } Separately, notice that each event handler is receiving the button input element’s id as an argument—this enables the event handler to use the id value when handling the event. } [Note: The localStorage and sessionStorage properties and methods we discuss throughout this section apply to both objects.]
Rights Reserved. 65 } Function clearAllSearches is called when the user clicks the Clear All Saved Searches button. } The clear method of the localStorage object removes all key/ value pairs from the object. } loadSearches is called to refresh the list of saved searches in the web page. } Function saveSearch is called when the user clicks Save to save a search. } The setItem method stores a key/value pair in the localStorage object. § If the key already exits, setItem replaces the corresponding value; § otherwise, it creates a new key/value pair. } loadSearches is called to refresh the list of saved searches in the web page. } removeItem method is called to remove a key/value pair from the localStorage object.
Rights Reserved. 71 } To appreciate the simplicity of JSON data, examine this representation of an array of address-book entries that we’ll use in Chapter 16: [ { first: 'Cheryl', last: 'Black' }, { first: 'James', last: 'Blue' }, { first: 'Mike', last: 'Brown' }, { first: 'Meg', last: 'Gold' } ] } JSON provides a straightforward way to manipulate objects in JavaScript, and many other programming languages now support this format. } In addition to simplifying object creation, JSON allows programs to easily extract data and efficiently transmit it across the Internet.