Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives.

Similar presentations


Presentation on theme: "Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives."— Presentation transcript:

1 Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives are their properties and verbs are their methods. Types of objects -User defined objects. -Core or built-in objects (Date, String, Number) -Browser and Document objects.

2 Dot Syntax A dot is used to separate the objects when descending the hierarchical tree-like structure used to describe all of the components of an object. Constructor A constructor is a method that creates an instance of an object. JavaScript comes with several built-in constructors. The new keyword precedes the name of the constructor that will be used to create the object. Examples of constructors: Object(), Array(), Date().

3 Object() The Object() constructor returns a reference to an object. The return value is assigned to a variable. Example The Object() Constructor var pet = new Object(); alert(pet);

4 Properties of the Object Any object subordinate to another object is also a property of that object. The properties assigned to the object are not variables and are not defined with the var keyword. Example The Object() Constructor var pet = new Object(); alert(pet); pet.cat = new Object(); alert(pet.cat); pet.cat.name = "Sneaky"; pet.cat.color = "yellow"; pet.cat.size = "fat"; pet.cat.attitude = "stuckup";

5 Methods of the Object Methods are special functions that are used to describe how object behaves or acts: pet.cat.play(); // no parameters pet.dog.fetch(ball); // passing parameters Format var myobj = new Object();

6 Example User-defined objects var toy = new Object(); // Create the object toy.name = "Lego"; // Assign properties to the object toy.color = "red"; toy.shape = "rectangle"; document.write(" The toy is a " + toy.name + "."); document.write(" It is a " + toy.color + " " + toy.shape+ ".");

7 Creating an Object with a User-Defined Function You can create a function that specifies the object’s name, properties, and methods. The function serves as a template or prototype of an object. The this keyword is used to refer to the object that has been passed to a function.

8 Example User-defined objects function book(title, author, publisher){ // Defining properties this.title = title; this.author = author; this.publisher = publisher; } var myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall"); document.writeln(" " + myBook.title + " " + myBook.author + " " + myBook.publisher);

9 Defining methods for an Object Simple Methods function distance(r, t){ // define the object this.rate = r; // assign properties this.time = t; } function calc_distance(){ // define a function that will be used as a method return this.rate * this.time; } var speed=eval(prompt("What was your speed (miles per hour)? ","")); var elapsed=eval(prompt("How long did the trip take (hours)?","")); var howfar=new distance(speed, elapsed); // call the constructor howfar.distance=calc_distance; // create a new property var d = howfar.distance(); // invoke method alert("The distance is " + d + " miles.");

10 A Method defined in a constructor User-defined objects function book(title, author, publisher){ // Receiving parameters this.pagenumber=0; // Properties this.title = title; this.author = author; this.publisher = publisher; this.uppage = pageForward; // Assign function name to a property this.backpage = pageBackward; } function pageForward(){ // Functions to be used as methods this.pagenumber++; return this.pagenumber; } function pageBackward(){ this.pagenumber--; return this.pagenumber; }

11 A Method defined in a constructor (continued) var myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall" ); //Create new object myBook.pagenumber=5; document.write( " "+ myBook.title + " " + myBook.author + " " + myBook.publisher + " Current page is " + myBook.pagenumber ); document.write(" Page forward: " ); for(i=0;i<3;i++){ document.write(" " + myBook.uppage()); // Move forward a page } document.write(" Page backward: "); for(;i>0; i--){ document.write(" " + myBook.backpage()); // Move back a page }

12 Properties can be objects Properties Can be Objects var pet = new Object(); // pet is an object pet.cat = new Object(); // cat is a property of the pet; object.cat is also an object pet.cat.name="Sylvester"; // cat is assigned properties pet.cat.color="black"; pet.dog = new Object(); pet.dog.breed = "Shepherd"; pet.dog.name = "Lassie"; document.write(" The cat's name is " + pet.cat.name + "."); document.write(" The dog's name is " + pet.dog.name + ".");

13 Object Literals Object created by assigning it a comma- separated list of properties enclosed in curly braces. FORMAT var object = {property1: value, property2: value};

14 Example Object Literals var car = { make: "Honda", year: 2002, price: 30000, owner: "Henry Lee"}; var details=car.make + " "; details += car.year + " "; details += car.price + " "; details += car.owner + " "; document.write(details);

15 With keyword The with keyword is used for referencing an object’s properties or methods Example The with Keyword function book(title, author, publisher){ this.title = title; // properties this.author = author; this.publisher = publisher; this.show = display; // Define a method } function display(anybook){ with(this){ // The with keyword var info = "The title is " + title; info += "\nThe author is " + author; info += "\nThe publisher is " + publisher; alert(info); }

16 Example (continued) var childbook = new book("A Child's Garden of Verses", "Robert Lewis Stevenson", "Little Brown"); var adultbook = new book("War and Peace", "Leo Tolstoy", "Penguin Books"); childbook.show(childbook); // call method for child's book adultbook.show(adultbook); // call method for adult’s book

17 The for/in loop can be used to iterate through a list of object properties or array elements FORMAT for (var property_name in object){ statements }

18 Example User-defined objects function book(title, author, publisher){ this.title = title; this.author = author; this.publisher = publisher; this.show=showProps; // define a method for the object } function showProps(obj, name){ // function to show the object's properties var result = ""; for (var prop in obj){ result += name + "." + prop + " = " + obj[prop] + " "; } return result; } myBook = new book("JavaScript by Example", "Ellie", "Prentice Hall"); document.write(" " + myBook.show(myBook, "myBook"));

19 prototypes OO languages support a feature called inheritance, where an object can inherit the properties of another. You can add properties to objects after they have been created by using the prototype objects. In OO, the object’s data describes the properties. The object, along with its properties and methods, is bundled up into a container called a class. Each JavaScript class has a prototype object and one set of properties.

20 Example User-defined objects and Inheritance function Book(title, author, publisher){ // The Book class this.title = title; this.author = author; this.publisher = publisher; this.show=showProps; } function showProps(obj,name){ var result = ""; for (var i in obj){ result += name + "." + i + " = " + obj[i] + " "; } return result; }

21 Example (continued) // Add a new function function lastEdition(){ this.latest=prompt("Enter the latest edition for "+this.title,""); return (this.latest); } // Add a new property with prototype Book.prototype.edition=lastEdition; var myBook=new Book("JavaScript by Example", "Ellie", "Prentice Hall"); // Define a new method document.write(" " + myBook.show(myBook,"myBook")+" "); document.write("The latest edition is "+ myBook.edition()+" ");

22 Extending a JavaScript Object Since all objects have the prototype object, it is possible to extend the properties of a JavaScript built-in object, just as we did for a user-defined object.

23 Example Prototypes // Customize String Functions function uc(){ var str=this.big(); return( str.toUpperCase()); } function lc(){ var str=this.small(); return( str.toLowerCase()); } String.prototype.bigUpper=uc; String.prototype.smallLower=lc; var string="This Is a Test STRING."; string=string.bigUpper(); document.write(string+" "); document.write( string.bigUpper()+" "); document.write(string.smallLower()+" ");

24 The Name Grabber Program Name Grabber function copyName() { var userName = document.myForm.txtName.value; var greeting = "Hi there, "; greeting += userName; greeting += "!"; document.myForm.txtGreeting.value = greeting; }

25 The Name Grabber Program Name Grabber Please type your name: <input type = "text" name = "txtName"> <input type = "button" value = "click me" onClick = 'copyName()'> <input type = "text" name = "txtGreeting">

26 var userName = document.myForm.txtName.value; The net result of this line is to copy the value of txtName to the userName variable. Document.myForm.txtName.value essentially becomes a variable that you can assign values to and from, just like any other variable. Once you understand this, the following line makes perfect sense as well: document.myForm.txtGreeting.value = greeting;


Download ppt "Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives."

Similar presentations


Ads by Google