1 / 16

Java Script

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.

theta
Download Presentation

Java Script

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Script Objects

  2. 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

  3. 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

  4. 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!

  5. 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

  6. 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)

  7. 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)

  8. 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");  }

  9. 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

  10. 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”

  11. 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”)

  12. 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

  13. 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

  14. 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.

  15. 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?

  16. 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”

More Related