Presentation is loading. Please wait.

Presentation is loading. Please wait.

M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.

Similar presentations


Presentation on theme: "M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for."— Presentation transcript:

1 M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk

2 Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for it, unlike the basic data types  It allows the variables (objects) to make use of the built-in properties and functions defined for them  For example Number, String, Date, Array etc

3 User defined objects  Along with the built-in object types, the users can also define their own type of objects.  They are defined with some data members and member functions  Unlike classes in other programming languages, each object has to be re-defined with their own set of attributes and behavior  However, an object can be assigned to another object to get the same structure and values

4 Example var person=new Object(); person.firstname="John"; person.lastname="Doe"; person.age=50; person.eyecolor="blue";

5 Creating objects with constructor function person(firstname, lastname, age, eyecolor) { this.firstname = firstname; this.lastname = lastname; this.age = age; this.eyecolor = eyecolor; } //Creating objects of type person var p1 = new person(“John”, “Doe”, 50, “blue”); var p2 = new person(“Sally”, “Rally”, 48, “green”);

6 Adding Member Functions function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.changeName=changeName; function changeName(name) { this.lastname=name; } } p1 = new person(“John”, “Doe”, 50, “blue”); //The member function changeName() changes the lastname of the object person p1.changeName(“David”);

7 Built-in Objects

8 Arrays var myCars=new Array("Saab","Volvo","BMW"); //array data members var total = myCars.length; //member functions to get index of value var y=myCars.indexOf("Volvo"); //if the given value is a number or has alphabets if(isNaN(myCars[1])); //convert array items into a comma separated string var allCars = myCars.join(); document.write(“ ” + allCars + “ ”);

9 var myCars=new Array("Saab","Volvo","BMW"); //push an item at the end of the array myCars.push(“Toyota”); //Pop an item from the end of an array myCars.pop(); //remove an item from the start of the array myCars.shift(); //convert array items into a comma separated string var allCars = myCars.join(); document.write(“ ” + allCars + “ ”); //js use + to concate

10 var myCars=new Array("Saab","Volvo","BMW"); var otherCars = new Array(“Toyota”, “Honda”, “Suzuki”); // concate two arrays into one var totalCars = myCars.concate(otherCars); //reverse the order of the elements in the array myCars.reverse(); //get second and third items from the array var moreCars = myCars.slice(1,3); //sort the array alphabetically myCars.sort();

11 var myCars=new Array("Saab","Volvo","BMW"); // add more items into the array myCars.splice(2,0,“Ford",“Nissan"); //get a comma separated string of array items var cars = myCars.toString();

12 Date object //instantiating date type objects var currDate = new Date(); var someDate = new Date(milliseconds) //since 1970/01/01 var otherDate = new Date (“2014/02/28”); //date string var anotherDate = new Date( year, month, day, hours, mins, seconds, milliseconds ); //Date objects can be compared using relational operators,= etc.

13 Date member functions Var myDate = new Date(); document.write(myDate.getFullYear); document.write(myDate.getTime); document.write(myDate.getDay);

14 Math Object //Math object has data members and member functions for mathematical relations document.write(“ ”+ Math.PI + “ ”); //Note: Javascript is case-sensitive math.pi is not the same as Math.PI document.write(“ ”+Math.round(2.39)+“ ”); //generates a random number between 0 and 1 document.write(“ ”+Math.random()+“ ”); //Get Maximum Value Document.write(“ ”+Math.max(5,10,15,2,43,16) + “ ”); //Get Minimum Value document.write(“ ”+Math.min(5,10,15,2,43,16) + “ ”); //Get Square root Document.write(“ ”+Math.sqrt(16) + ”);

15 String Objects var venue = “attock”; //get a character at the given index document.write(“ ”+venue[3]+” ”); //length of string document.write(“ ”+venue.length+” ”); var ind = venue.indexOf(“t”); //index of characters match first found var ind2 = venue.lastIndexOf(“t”); //index of characters match last found var word = venue.match(“ock”); //returns the match term if found or null venue.replace(“oc”, “zz”); //replace characters in 1 st parameters with 2 nd venue.toUpperCase(); //convert string to upper case venue.toLowerCase(); //convert string to lower case

16 //convert the string to array splitting on given term var venues = “attock:wah:islamabad:lahore”; var allVenues = venues.split(“:”); //Other String functions charAt(), concat(), search(), slice(), substr(), substring(), trim(), valueOf()


Download ppt "M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for."

Similar presentations


Ads by Google