Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
-
Upload
cathleen-dean -
Category
Documents
-
view
232 -
download
0
Transcript of Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
![Page 1: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/1.jpg)
Chapter 6
Object-Oriented Java Script
JavaScript, Third Edition
![Page 2: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/2.jpg)
JavaScript, Third Edition 2
Objectives
• Study object-oriented programming
• Learn about the built-in JavaScript objects
• Work with the Array, Date, Math, and Number objects
• Define custom JavaScript objects
![Page 3: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/3.jpg)
JavaScript, Third Edition 3
Introduction to Object-Oriented Programming
• Object-oriented programming allows you to reuse code without having to copy or recreate it
![Page 4: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/4.jpg)
JavaScript, Third Edition 4
Reusable Software
• Object-oriented programming (OOP):
– Refers to the creation of reusable software objects that can be easily incorporated into multiple programs
• Object:
– Specifically refers to programming code and data that can be treated as an individual unit or component
![Page 5: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/5.jpg)
JavaScript, Third Edition 5
Reusable Software (Cont.)
• Data:
– Refers to information contained within variables or other types of storage structures
• Objects can range from simple controls such as a button, to entire programs such as a database application
![Page 6: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/6.jpg)
JavaScript, Third Edition 6
Reusable Software (Cont.)
• Popular object-oriented programming languages include C++, Java, and Visual Basic
• Using any of these or other object-oriented languages:
– Programmers can create objects themselves or use objects created by other programmers
![Page 7: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/7.jpg)
JavaScript, Third Edition 7
Encapsulation
• Objects are encapsulated:
– All code and required data are contained within the object itself
• Encapsulation:
– Places code inside what programmers like to call a “black box”
– When an object is encapsulated, you cannot see “inside” it—all internal workings are hidden
![Page 8: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/8.jpg)
JavaScript, Third Edition 8
Encapsulation (Cont.)
• The code (methods and statements) and data (variables and constants) contained in an encapsulated object are accessed through an interface
• Interface:
– Represents elements required for a source program to communicate with an object
![Page 9: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/9.jpg)
JavaScript, Third Edition 9
Encapsulation (Cont.)
• The principal of information hiding states that:
– Any class members that other programmers do not need to access or know about should be hidden
![Page 10: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/10.jpg)
JavaScript, Third Edition 10
Encapsulation (Cont.)
• Encapsulation prevents other programmers from:
– Accidentally introducing a bug into a program
– Stealing the code and claiming it as their own
![Page 11: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/11.jpg)
JavaScript, Third Edition 11
Encapsulation (Cont.)
![Page 12: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/12.jpg)
JavaScript, Third Edition 12
Classes • In object-oriented programming:
– Code, methods, attributes, and other information that make up an object are organized into classes
• Class:
– Template, or blueprint, that serves as the basis for new objects
• Instance:
– Object that has been created from an existing class
• Called instantiating the object
![Page 13: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/13.jpg)
JavaScript, Third Edition 13
Classes (Cont.)
• A particular instance of an object inherits its methods and properties from a class:
– It takes on the characteristics of the class on which it is based
![Page 14: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/14.jpg)
JavaScript, Third Edition 14
Built-in JavaScript Classes
![Page 15: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/15.jpg)
JavaScript, Third Edition 15
Instantiating an Object
• You can use some of the built-in JavaScript objects directly in your code
• Other objects require you to instantiate a new object
![Page 16: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/16.jpg)
JavaScript, Third Edition 16
Garbage Collection
• Refers to cleaning up, or reclaiming, memory that is reserved by a program
• JavaScript knows when a program no longer needs a variable or object
– Automatically cleans up the memory
![Page 17: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/17.jpg)
JavaScript, Third Edition 17
Array Class
• The Array class creates new array objects
• When creating an array using the Array() constructor, an object from the Array class is instantiated
• In addition to the Array() constructor, the Array class contains:
– Methods and properties for manipulating the elements of an array
![Page 18: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/18.jpg)
JavaScript, Third Edition 18
Array Class Methods
![Page 19: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/19.jpg)
JavaScript, Third Edition 19
The Length Property
• Returns the number of elements in an array
• You append the length property to the name of the array you want to sort using the following syntax:
array_name.length;
![Page 20: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/20.jpg)
JavaScript, Third Edition 20
Date Class
• Contains methods and properties for manipulating the date and time
• The Date object allows you to use the current date and time in your JavaScript programs
• You create a new instance of the Date class using the syntax var dateObject = new Date();
![Page 21: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/21.jpg)
JavaScript, Third Edition 21
Date Class (Cont.)
![Page 22: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/22.jpg)
JavaScript, Third Edition 22
Number Class
• Contains methods for manipulating numbers and properties that contain static values representing some of the numeric limitations in the JavaScript language
![Page 23: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/23.jpg)
JavaScript, Third Edition 23
Number Class Methods
![Page 24: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/24.jpg)
JavaScript, Third Edition 24
Number Class Properties
![Page 25: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/25.jpg)
JavaScript, Third Edition 25
Math Class
• The Math class contains methods and properties for performing mathematical calculations in your programs
![Page 26: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/26.jpg)
JavaScript, Third Edition 26
Math Class Methods
![Page 27: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/27.jpg)
JavaScript, Third Edition 27
Math Class Methods (Cont.)
![Page 28: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/28.jpg)
JavaScript, Third Edition 28
Math Class Properties
![Page 29: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/29.jpg)
JavaScript, Third Edition 29
Custom JavaScript Objects
• JavaScript is not a true object-oriented programming language:
– Base objects in your programs on built-in JavaScript classes such as the Array and Date objects
– However, you cannot create your own classes in JavaScript
– You can define your own custom objects using a constructor function
![Page 30: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/30.jpg)
JavaScript, Third Edition 30
Constructor Function
• A function used as the basis for a custom object
• JavaScript objects inherit all the variables and statements of the constructor function on which they are based
• Any JavaScript function can serve as a constructor
![Page 31: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/31.jpg)
JavaScript, Third Edition 31
Properties
• To add a property to a constructor function:
– You must add a statement to the function body that uses the this keyword with the following syntax:
• this.property_name = value;
– In the case of a custom JavaScript object, the this keyword refers to the object that calls the constructor function
![Page 32: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/32.jpg)
JavaScript, Third Edition 32
Methods
• You can create a function that will be used as an object method:
– By referring to any object properties using the this reference
![Page 33: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/33.jpg)
JavaScript, Third Edition 33
The prototype Property
• The prototype property is a built-in property:
– Specifies the constructor from which an object was
extended
![Page 34: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/34.jpg)
JavaScript, Third Edition 34
Chapter Summary
• Object-Oriented programming (OOP):
– Refers to the creation of reusable software objects that can be easily incorporated into another program
• Components:
– Reusable software objects
• Object:
– Programming code and data that can be treated as an individual unit or component
![Page 35: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/35.jpg)
JavaScript, Third Edition 35
Chapter Summary (cont.)
• Objects are encapsulated:
– All code and required data are contained within the object itself
• Interface :
– Represents elements required for a source program to communicate with an object
• The principal of information hiding:
– States that any class members that other programmers do not need to access or know about should be hidden
![Page 36: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/36.jpg)
JavaScript, Third Edition 36
Chapter Summary (cont.)• In object-oriented programming:
– Code, methods, attributes, and other information that make up an object are organized using classes
• An instance:– Object created from an existing class
• An object inherits, or takes on, the characteristics of the class on which it is based
• The Date class:– Contains methods and properties for manipulating the
date and time
![Page 37: Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.](https://reader036.fdocuments.in/reader036/viewer/2022081511/56649ec65503460f94bd1172/html5/thumbnails/37.jpg)
JavaScript, Third Edition 37
Chapter Summary (cont.)
• The Number class:
– Contains methods for manipulating numbers and properties that
• The this keyword:
– Refers to the current object
• The prototype property:
– Built-in property that specifies the constructor from which an object was extended