1 / 16

Javascript Objects

M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk. Javascript Objects. Javascript Objects. Every data-type defined in Javascript is an object It has a class definition for it, unlike the basic data types

onawa
Download Presentation

Javascript Objects

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. M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk Javascript Objects

  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 <script type=“text/javascript”> var person=new Object();person.firstname="John";person.lastname="Doe";person.age=50;person.eyecolor="blue"; </script>

  5. Creating objects with constructor <script type=“text/javascript”> 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”); </script>

  6. Adding Member Functions <script type=“text/javascript”> 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”); </script>

  7. Built-in Objects

  8. Arrays <script type=“text/javascript”> varmyCars=new Array("Saab","Volvo","BMW"); //array data members var total = myCars.length; //member functions to get index of value vary=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 varallCars = myCars.join(); document.write(“<p>” + allCars + “</p>”); </script>

  9. <script type=“text/javascript”> varmyCars=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 varallCars = myCars.join(); document.write(“<p>” + allCars + “</p>”); //js use + to concate </script>

  10. <script type=“text/javascript”> varmyCars=new Array("Saab","Volvo","BMW"); varotherCars = new Array(“Toyota”, “Honda”, “Suzuki”); // concate two arrays into one vartotalCars = myCars.concate(otherCars); //reverse the order of the elements in the array myCars.reverse(); //get second and third items from the array varmoreCars = myCars.slice(1,3); //sort the array alphabetically myCars.sort(); </script>

  11. <script type=“text/javascript”> varmyCars=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(); </script>

  12. Date object <script type=“text/javascript”> //instantiating date type objects varcurrDate = new Date(); varsomeDate = new Date(milliseconds) //since 1970/01/01 varotherDate = new Date (“2014/02/28”); //date string varanotherDate = new Date(year, month, day, hours, mins, seconds, milliseconds); //Date objects can be compared using relational operators <,>,= etc. </script>

  13. Date member functions <script type=“text/javascript”> VarmyDate = new Date(); document.write(myDate.getFullYear); document.write(myDate.getTime); document.write(myDate.getDay); </script>

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

  15. String Objects <script type=“text/javascript”> var venue = “attock”; //get a character at the given index document.write(“<p>”+venue[3]+”</p>”); //length of string document.write(“<p>”+venue.length+”</p>”); varind = 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 1st parameters with 2nd venue.toUpperCase(); //convert string to upper case venue.toLowerCase(); //convert string to lower case </script>

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

More Related