JavaScript, Fourth Edition

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

The Web Warrior Guide to Web Design Technologies
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
PHP Programming with MySQL Slide 11-1 CHAPTER 11 Developing Object-Oriented PHP.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Review of C++ Programming Part II Sheng-Fang Huang.
Object Oriented Software Development
A First Program Using C#
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Objectives In this chapter, you will:
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
JavaScript Shopping Cart Yong Choi CSU Bakersfield.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Learners Support Publications Object Oriented Programming.
JavaScript, Fourth Edition
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Introduction to Object-Oriented Programming Lesson 2.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 10 Developing Object-Oriented PHP. 2 Objectives In this chapter, you will: Study object-oriented programming concepts Use objects in PHP scripts.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
JavaScript, Sixth Edition
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
Classes and Objects in Java
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Presentation transcript:

JavaScript, Fourth Edition Chapter 6 Using Object-Oriented JavaScript

Objectives Study object-oriented programming Learn about the built-in JavaScript objects Work with the Date, Number, and Math objects Define custom JavaScript objects JavaScript, Fourth Edition JavaScript, Fourth Edition 2 2

Introduction to Object-oriented Programming Allows you to reuse code without having to copy or recreate it JavaScript, Fourth Edition

Reusing Software Objects Object-oriented programming (OOP) Creating reusable software objects Easily incorporated into multiple programs Object Programming code and data that can be treated as an individual unit or component Also called component Data Information contained within variables or other types of storage structures JavaScript, Fourth Edition

Reusing Software Objects (continued) Objects range from simple controls to entire programs Popular object-oriented programming languages C++, Java, and Visual Basic JavaScript, Fourth Edition 5

Reusing Software Objects (continued) JavaScript, Fourth Edition 6

What is Encapsulation? Objects are encapsulated Code and data are contained within the object itself Encapsulation places code inside a “black box” Interface Elements required for program to communicate with an object Principle of information hiding Any methods and properties that other programmers do not need to access should be hidden JavaScript, Fourth Edition

What is Encapsulation? (continued) JavaScript, Fourth Edition 8

Understanding Classes Code, methods, attributes, and other information that make up an object Instance Object that has been created from an existing class Instantiate: create an object from an existing class An instance of an object inherits its methods and properties from a class JavaScript, Fourth Edition 9

Understanding Classes (continued) Objects in the browser object model Part of the Web browser You do not need to instantiate them to use them JavaScript, Fourth Edition 10

Built-in JavaScript Classes JavaScript, Fourth Edition 11

Instantiating an Object You can use some of the built-in JavaScript objects directly in your code Some objects require you to instantiate a new object Example Math.PI var teamRoster = new Array(); JavaScript, Fourth Edition 12

Performing Garbage Collection Cleaning up, or reclaiming, memory that is reserved by a program When you declare a variable or instantiate a new object You are reserving memory for the variable or object JavaScript knows when your program no longer needs a variable or object And automatically cleans up the memory for you JavaScript, Fourth Edition 13

Using the Date Class Date class Example Methods and properties for manipulating the date and time Allows you to use a specific date or time element in your JavaScript programs Example var today = new Date(); var mozartsBirthday = new Date(1756, 0, 27); Example: Central Valley Snowboarding Web page Visitors can use to make group reservations Use the Date object to generate a monthly calendar JavaScript, Fourth Edition 14

Using the Date Class (continued) JavaScript, Fourth Edition 15

Using the Date Class (continued) JavaScript, Fourth Edition 16

Using the Date Class (continued) JavaScript, Fourth Edition 17

Using the Date Class (continued) JavaScript, Fourth Edition 18

Manipulating Numbers with the Number Class Methods for manipulating numbers and properties that contain static values Representing some of the numeric limitations in the JavaScript language You can simply append the name of any Number class method or property To the name of an existing variable that contains a numeric value JavaScript, Fourth Edition 19

Manipulating Numbers with the Number Class (continued) JavaScript, Fourth Edition 20

Accessing Number Class Properties Example: Central Valley Snowboarding Web page Add code to the Group Reservations page that calculates group discounts JavaScript, Fourth Edition 21

Accessing Number Class Properties (continued) JavaScript, Fourth Edition 22

Performing Math Functions with the Math Class Methods and properties for mathematical calculations JavaScript, Fourth Edition 23

Using Math Class Methods JavaScript, Fourth Edition 24

Accessing Math Class Properties JavaScript, Fourth Edition 25

Accessing Math Class Properties (continued) Example: Central Valley Snowboarding Web page Modify the calcGroupDiscount() function to use the round() function of the Math object To round the group discount to the nearest integer instead of displaying decimal places JavaScript, Fourth Edition 26

Defining Custom JavaScript Objects JavaScript is not a true object-oriented programming language You cannot create your own classes in JavaScript JavaScript is an object-based language You can define your own custom objects Custom objects in JavaScript are not encapsulated JavaScript, Fourth Edition 27

Declaring Basic Custom Objects Use the Object object Syntax var objectName = new Object(); var objectName = {}; You can assign properties to the object Append the property name to the object name with a period Custom objects created as described in this section are limited to containing only properties Objects are most useful when they contain both properties and methods JavaScript, Fourth Edition

Declaring Basic Custom Objects (continued) Create custom objects that contain methods Use constructor functions Example: Central Valley Snowboarding Web page Start adding a Group Members section Allows you to enter information about each snowboarder in the group JavaScript, Fourth Edition 29

Declaring Basic Custom Objects (continued) JavaScript, Fourth Edition 30

Defining Constructor Functions Used as the basis for a custom object Also known as object definition JavaScript objects Inherit all the variables and statements of the constructor function on which they are based Syntax: var newObject = new function(); Any JavaScript function can serve as a constructor Example: Central Valley Snowboarding Web page Add a constructor function to the Group Reservations page JavaScript, Fourth Edition 31

Adding Properties Add a statement to the function body that uses the this keyword with the following syntax: this.property_name = value; this keyword refers to the object that calls the constructor function Example Add properties to the Contact constructor function JavaScript, Fourth Edition 32

Enumerating Custom Object Properties Custom objects can contain dozens of properties To execute the same statement or command block for all the properties within a custom object Use the for...in statement for...in statement Looping statement similar to the for statement Syntax for (variable in object) { statement(s); } JavaScript, Fourth Edition 33

Enumerating Custom Object Properties (continued) for...in statement enumerates, or assigns an index to, each property in an object Example for (prop in ticketOrder) { document.write(ticketOrder[prop] + "<br />"); } Start adding a function named addContact() that will add snowboarders to the contact list JavaScript, Fourth Edition 34

Referring to Object Properties as Associative Arrays An array whose elements are referred to with an alphanumeric key instead of an index number You can also use associative array syntax to refer to the properties of an object Syntax ObjectName[“PropertyName”]; With associative arrays you can dynamically build property names at runtime Example Complete the addContact() function JavaScript, Fourth Edition 35

Deleting Properties Use the delete operator with the syntax Example delete object.property Example Add a deleteContact() function to the Group Reservations page That deletes selected snowboarders from the Group Members section JavaScript, Fourth Edition 36

Creating Methods Create a function that will be used as an object method By referring to any object properties it contains with the this reference Method must be added to the constructor function Using the syntax this.methodName = functionName; methodName is the name that is being assigned to the function within the object Example Add methods to the Contact constructor function JavaScript, Fourth Edition

Using the prototype Property After instantiating a new object You can assign additional properties to the object, using a period New property is only available to that specific object prototype property Built-in property that specifies the constructor from which an object was instantiated When used with the name of the constructor function Any new properties you create will also be available to the constructor function JavaScript, Fourth Edition 38

Using the prototype Property (continued) Object definitions can use the prototype property to extend other object definitions You can create a new object based on an existing object JavaScript, Fourth Edition 39

Summary Object-oriented programming (or OOP) refers to the creation of reusable software objects Reusable software objects are called components An object is programming code and data that can be treated as an individual unit or component Objects are encapsulated An interface represents elements required for a source program to communicate with an object JavaScript, Fourth Edition

Summary (continued) Principle of information hiding Code, methods, attributes, and other information that make up an object are organized using classes An instance is an object that has been created from an existing class An object inherits the characteristics of the class on which it is based The Date class contains methods and properties for manipulating the date and time JavaScript, Fourth Edition

Summary (continued) The Number class contains methods for manipulating numbers and properties The Math class contains methods and properties for performing mathematical calculations You can define your own custom objects using a constructor function The this keyword refers to the current object The prototype property JavaScript, Fourth Edition 42