🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Javascript Module 2 Objects Everywhere you look in the real world you see objects—people, animals, plants, cars, planes, buildings, computers, monitors and so on. In web browers ,objects are browser window itself ,forms,buttons,textboxes…...

Javascript Module 2 Objects Everywhere you look in the real world you see objects—people, animals, plants, cars, planes, buildings, computers, monitors and so on. In web browers ,objects are browser window itself ,forms,buttons,textboxes… ARUL VIT SCOPE Methods:- action performed by the object - a car accelerates, brakes and turns; -a ball rolls, bounces, inflates and deflates -window object can alert the user using “alerts” 2 Objects.. All objects have their own properties -- car has wheels --browser has a name ,version number ARUL VIT SCOPE Object system –simple & flexible -- can add new properties and methods at any time --unordered collection of values 3 Objects.. String Object Math Object Boolean & Number Object ARUL VIT SCOPE Document Object Window Object 4 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 $. ARUL VIT SCOPE JavaScript supports the set of characters called Unicode®, which represents a large portion of the world’s languages. A string is an object of type String 5 String objects.. 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, as follows: "John Q. Doe" (a name) ARUL VIT SCOPE '9999 Main Street' (a street address) "Waltham, Massachusetts" (a city and state) '(201) 555-1212' (a telephone number) A String may be assigned to a variable in a declaration. The declaration var color = "blue"; 6 String objects.. Strings can be compared via the relational (=) and equality operators (== and !=). Strings are compared using the Unicode ARUL VIT SCOPE values of the corresponding characters. For example, the expression "hello" < "Hello" evaluates to false because lowercase letters have higher Unicode values. 7 String Methods.. Format : stringname.methodname(); str.toUppercase(); Selecting characters from string : charAt(index) ARUL VIT SCOPE returns characer at specified index.first character at index 0 – charCodeAt(index) returns unicode value/no character at that index then NaN Combining strings: concat ( string ); Replacing string values: replace(searchstring,replacestring); 8 String Methods fromcharCode(value1,value2…); – The String.fromCharCode method in JavaScript is used to create a string from a sequence of Unicode values. – It's a static method of the String object, which means it's always called as String.fromCharCode() document.write("" +String.fromCharCode(86,73,84)); String Methods.. Obtaining substrings of a string slice(Start,end); substr(start,length); substring(start,end); ARUL VIT SCOPE Searching for substring within the string indexOf(substring,index); lastIndexOf(substring,index); Converting strings to all lower /upper case: toLowercase(); toUppercase(); 11 The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it returns -1 if the string is not found The lastIndexOf(searchstring,index) methods searches backwards (from the end to the beginning), meaning: if the second parameter is 15, the search starts at position 15, and searches to the beginning of the string. split( string ) Splits the source string into an array of strings (tokens), where its string argument specifies the delimiter (i.e., the characters that indicate the end of each token in the source string). Date Object JavaScript’s Date object provides methods for date and time manipulations. Date and time processing can be performed based on the computer’s local time zone or based on World ARUL VIT SCOPE Time Standard’s Coordinated Universal Time (abbreviated UTC) 16 Date Object Initializes Date object with current date & time Allocates memory for object Calls Date object constructor var current = new Date(); ARUL VIT SCOPE document.write(“date is:”+current.getDate()); 17 Universal Time Coordinated 18 19 20 var current = new Date(); document.writeln("String representations and valueOf" ); document.writeln( "toString: " +current.toString() + "toLocaleString: " +current.toLocaleString() + "toUTCString: " +current.toUTCString() + "valueOf: " +current.valueOf()); document.writeln("Get methods for local time zone" ); document.writeln("getDate: " +current.getDate() + "getDay: " +current.getDay() + "getMonth: " + current.getMonth()+ "getFullYear: " +current.getFullYear() + "getTime: " +current.getTime() + "getHours: " +current.getHours() + "getMinutes: " +current.getMinutes() + "getSeconds: " +current.getSeconds() + "getMilliseconds: " +current.getMilliseconds()); document.writeln("Specifying arguments for a new Date" ); var anotherDate = new Date( 2007, 2, 18, 1, 5, 0, 0 ); //year,month,day,hours,minutes,seconds,ms document.writeln( "Date: " + anotherDate ); document.writeln( "Set methods for local time zone" ); anotherDate.setDate( 31 ); anotherDate.setMonth( 11 ); anotherDate.setFullYear( 2007 ); anotherDate.setHours( 23 ); anotherDate.setMinutes( 59 ); anotherDate.setSeconds( 59 ); document.writeln( "Modified date: " + anotherDate ); 23 Math Object.. The Math object’s 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 (.) and the name of the method. ARUL VIT SCOPE 24 Example.. To calculate and display the square root of 900.0 you might write document.writeln( Math.sqrt( 900.0 ) ); ARUL VIT SCOPE which calls method Math.sqrt to calculate the square root of the number contained in the parentheses (900.0), then outputs the result. The number 900.0 is the argument of the Math.sqrt method. The preceding statement would display 30.0 25 ARUL VIT SCOPE 26 Math Object The Math object defines several commonly used mathematical constants, ARUL VIT SCOPE 27 Boolean and Number Objects JavaScript provides the Boolean and Number objects as object wrappers for boolean true/ false values and numbers These wrappers define methods and properties useful in manipulating boolean values and ARUL VIT SCOPE numbers. 28 Boolean Objects JavaScript automatically creates a Boolean object to store the value. JavaScript programmers can create Boolean objects explicitly with the statement ARUL VIT SCOPE var b = new Boolean( booleanValue ); booleanValue specifies whether the value of the Boolean object should be true or false, NaN or an empty string or if no argument is supplied, the new Boolean object contains false. Otherwise,the new Boolean object contains true 29 Boolean Objects Number Objects JavaScript programmers can create a Number object with the statement var n = new Number( numericValue ); The constructor argument numericValue is the number to store in the object. ARUL VIT SCOPE You can explicitly create Number objects, normally the JavaScript interpreter creates them as needed 31 Number Objects Common methods & properties of window object. ARUL VIT SCOPE 34 Document Object The document object is used to manipulate the document that is currently visible in the browser window.(write (), writeln(), cookie ) ARUL VIT SCOPE document.write(string) -Writes the string to the HTML document as HTML code. document.writeln(String) -Writes the string to the HTML document as HTML code and adds a newline character at the end. getElementById( id ) -Returns theDOM node representing the HTML element whose id attribute 37 matches id. Document Object bgcolor,fgcolor- method that accepts either hexadecimal values or common names for colors ARUL VIT SCOPE document.bgColor=“#e302334”; document.fgColor= “coral”; 38

Use Quizgecko on...
Browser
Browser