Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects.  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.

Similar presentations


Presentation on theme: "Objects.  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."— Presentation transcript:

1 Objects

2  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  The Values associated with an object  For instance the Length property of the String object  var txt="Hello World!"; document.write(txt.length);  The output would be 12

4  These are the actions that can be performed on the objects  For example the toUpperCase() method of the String object  var str="Hello world!"; document.write(str.toUpperCase());  Here the output would be  HELLO WORLD!

5  Creating a Date Object  They are created with the Date() Constructor  There are 4 different ways of instantiating a date 1. new Date() // current date and time 2. new Date(milliseconds) //milliseconds since 1970/01/01 3. new Date(dateString) 4. new Date(year, month, day, hours, minutes, seconds, milliseconds)  Most parameters are optional. If you don’t specify it passes 0

6  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  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  var myDate=new Date(); myDate.setFullYear(2010,0,14);  Set the date to 5 days in to the future  var myDate=new Date(); myDate.setDate(myDate.getDate()+5);  (It is smart enough to know how many days are in a month)

8  You are able to compare dates using the Date Object  var myDate=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  Creating the Array  Can be done in 3 ways 1. var myCars=new Array(); // regular array (add an optional integer myCars[0]="Saab"; // argument to control array's size) myCars[1]="Volvo"; myCars[2]="BMW"; 2. var myCars=new Array("Saab","Volvo","BMW"); // condensed array 3. var myCars=["Saab","Volvo","BMW"]; // literal array

10  Access  document.write(myCars[0]);  This will output  Saab  Index Starts at 0  Modify:  myCars[0]="Opel";  Changes “Saab” to “Opel”

11  Can represent two values:  True  False  var myBoolean=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  Creating an instance of the Math object  var x=Math.PI; var y=Math.sqrt(16);  8 Mathematical constants to use  Math.E Math.PI Math.SQRT2 Math.SQRT1_2 Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E

13  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  An Object that describes a pattern of characters  Used to perforn pattern-matching and “search and replace” funtions  Syntax  var patt=new RegExp(pattern,modifiers); or more simply: var patt=/pattern/modifiers;  pattern specifies the pattern of an expression  modifiers specify if a search should be global, case-sensitive, etc.

15  Examples:  To do a case insensitive search for w3schools  var str = "Visit W3Schools";  var patt1 = /w3schools/i;  document.write(str.match(patt1));  Visit W3Schools  To do a global search for “is”  var str="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”  var str="Is this all there is?";  var patt1=/is/gi;  document.write(str.match(patt1));  Is this all there is?

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


Download ppt "Objects.  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."

Similar presentations


Ads by Google