160 likes | 439 Views
Java Script. Objects. Object Oriented Programming. Java Script is an OOP Language So it allows you to make your own objects and make your own variable types I will not be going over how to make your own object That’s within Ash’s section tomorrow :P. Properties.
E N D
Java Script Objects
Object Oriented Programming • Java Script is an OOP Language • So it allows you to make your own objects and make your own variable types • I will not be going over how to make your own object • That’s within Ash’s section tomorrow :P
Properties • The Values associated with an object • For instance the Length property of the String object • <script type="text/javascript">var txt="Hello World!";document.write(txt.length);</script> • The output would be 12
Methods • These are the actions that can be performed on the objects • For example the toUpperCase() method of the String object • <script type="text/javascript">varstr="Hello world!";document.write(str.toUpperCase());</script> • Here the output would be • HELLO WORLD!
Date Object • Creating a Date Object • They are created with the Date() Constructor • There are 4 different ways of instantiating a date • new Date() // current date and time • new Date(milliseconds) //milliseconds since 1970/01/01 • new Date(dateString) • new Date(year, month, day, hours, minutes, seconds, milliseconds) • Most parameters are optional. If you don’t specify it passes 0
Creating dates Cont. • All dates are calculated in milliseconds from 01 Jan 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds • Some examples of instantiating a date • var today = new Date() • var d1 = new Date("October 13, 1975 11:13:00") • var d2 = new Date(79,5,24) • var d3 = new Date(79,5,24,11,33,0)
Get and Set Dates • After you create the Date variable you can use get and set methods to manipulate it • For example set a Date object to a specific date. The 14th January 2010 • varmyDate=new Date();myDate.setFullYear(2010,0,14); • Set the date to 5 days in to the future • varmyDate=new Date();myDate.setDate(myDate.getDate()+5); • (It is smart enough to know how many days are in a month)
Comparing two Dates • You are able to compare dates using the Date Object • varmyDate=new Date();myDate.setFullYear(2010,0,14);var today = new Date();if (myDate>today) { alert("Today is before 14th January 2010"); }else { alert("Today is after 14th January 2010"); }
Arrays • Creating the Array • Can be done in 3 ways • varmyCars=new Array(); // regular array (add an optional integermyCars[0]="Saab"; // argument to control array's size)myCars[1]="Volvo";myCars[2]="BMW"; • varmyCars=new Array("Saab","Volvo","BMW"); // condensed array • varmyCars=["Saab","Volvo","BMW"]; // literal array
Accessing and modifying the arrays • Access • document.write(myCars[0]); • This will output • Saab • Index Starts at 0 • Modify: • myCars[0]="Opel"; • Changes “Saab” to “Opel”
A Boolean Object • Can represent two values: • True • False • varmyBoolean=new Boolean(); • If the constructor is sent no value or one of these: • 0 • -0 • null • "" • false • undefined • NaN • It is set to False • If it is sent anything else, it is set as true (Even “false”)
Math Object • Creating an instance of the Math object • var x=Math.PI;var y=Math.sqrt(16); • 8 Mathematical constants to use • Math.EMath.PIMath.SQRT2Math.SQRT1_2Math.LN2Math.LN10Math.LOG2EMath.LOG10E
Math Methods • Round() (To the nearest int) • document.write(Math.round(4.7)); • This will output 5 • Random() • document.write(Math.random()); • This outputs a random number between 0 and 1 • Floor() (Rounds down) • document.write(Math.floor(50.123) • This will output 50
Regular Expressions • An Object that describes a pattern of characters • Used to perforn pattern-matching and “search and replace” funtions • Syntax • varpatt=new RegExp(pattern,modifiers);or more simply:varpatt=/pattern/modifiers; • pattern specifies the pattern of an expression • modifiers specify if a search should be global, case-sensitive, etc.
RegExp Continued • Examples: • To do a case insensitive search for w3schools • varstr = "Visit W3Schools"; • var patt1 = /w3schools/i; • document.write(str.match(patt1)); • Visit W3Schools • To do a global search for “is” • varstr="Is this all there is?"; • var patt1=/is/g; • document.write(str.match(patt1)); • Is this all there is? • To do a global case insensitive search for “is” • varstr="Is this all there is?"; • var patt1=/is/gi; • document.write(str.match(patt1)); • Is this all there is?
Moar Regular Expressions • Test() Method • Searches for a specified value within a string and returns a boolean • var patt1=new RegExp("e");document.write(patt1.test("The best things in life are free")); • Returns true since there is a “e” in the string • Exec() Method • Searches for a specified value within a string and returns the value if found otherwise it returns null • var patt1=new RegExp("e");document.write(patt1.exec("The best things in life are free")); • Returns “e”