SlideShare a Scribd company logo
1 of 67
HTML5, CSS3,
and JavaScript
6th Edition
Exploring Object-Based Programming
Tutorial 14
Carey
XPXPXPXPXPObjectives
• Use nested functions
• Create an object literal
• Define object properties and methods
• Define an object class
• Use object constructor functions
• Instantiating an object
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 2
XPXPXPXPXPObjectives (continued)
• Define an object prototype
• Explore prototype chains
• Use the apply() and call() methods
• Work with objects and arrays
• Create a for…in loop
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 3
XPXPXPXPXPWorking with Nested Functions
• Any function, including named functions, can
be nested within another function as follows:
function outsideFn() {
commands
function insideFn() {
commands }
commands
}
where
– outsideFn() is the outer or containing function
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 4
XPXPXPXPXP
Working with Nested Functions
(continued 1)
– insideFn() is the inner or nested function
• Scope of a nested function is limited to the
commands within the containing function
• Nested function is hidden from other code in
the script, making the code contained and
easier to manage
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 5
XPXPXPXPXP
Working with Nested Functions
(continued 2)
• Example: Nested functions can be used in
developing the poker game app for Arthur’s
Games website
• The app displays the poker table on which
users will play a game of draw poker
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 6
XPXPXPXPXP
Working with Nested Functions
(continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 7
XPXPXPXPXP
Working with Nested Functions
(continued 4)
• All operations for the game will be stored
within the playDrawPoker() function
• playDrawPoker() function runs automatically
when the page loads
• playDrawPoker() function is created by adding
references to all the buttons on the poker
table
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 8
XPXPXPXPXP
Working with Nested Functions
(continued 5)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 9
XPXPXPXPXP
Working with Nested Functions
(continued 6)
• Elements on the Draw Poker page perform the
following tasks:
– Deal button deals five cards from the poker deck
into the player’s hand
– Draw button replaces all selected cards in the
player’s hand with new cards from the deck
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 10
XPXPXPXPXP
Working with Nested Functions
(continued 7)
– Stand button signals to the dealer that the player
wants to keep all the cards in the dealt hand
– Reset button restarts the game with a fresh pot,
resetting the bank value to $500
– Bet selection list places the bet before the next
hand is dealt
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 11
XPXPXPXPXP
Working with Nested Functions
(continued 8)
• The Deal, Draw, and Stand buttons and the Bet
selection list will be turned on and off
depending on the state of the game
• The Draw and Stand buttons are disabled
before the deal
• The Deal button and Bet selection list are
disabled while the current hand is in play
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 12
XPXPXPXPXP
Working with Nested Functions
(continued 9)
• To disable and enable the buttons, nest the
required functions within the
playDrawPoker() function
• The functions for disabling or enabling the
selected object also set the opacity style of the
selected object
• Disabled objects are semi-transparent on the
page
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 13
XPXPXPXPXP
Working with Nested Functions
(continued 10)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 14
XPXPXPXPXP
Working with Nested Functions
(continued 11)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 15
XPXPXPXPXPIntroducing Custom Objects
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 16
• There are three kinds of JavaScript objects
– Native objects, such as Date or Array objects,
are part of the JavaScript language
– Host objects are objects provided by the browser
for use in interacting with the web document and
browser, such as the window, document, or form
objects
– Custom objects, also known as user-defined
objects, are objects created by the user for specific
programming tasks
XPXPXPXPXPIntroducing Custom Objects (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 17
• The following custom objects are created for
the poker game application:
– A poker game object that contains information
about the card game being played
– A poker deck object that contains information
about the cards used in the game
– A poker hand object that contains information
about the hand played by the user in the game
– Poker card objects that contain information about
the individual cards in the poker hand
XPXPXPXPXPIntroducing Custom Objects (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 18
• A custom object can be defined in one of the
following three ways:
– Creating it as an object literal
– Using an object constructor
– Applying the object create() method
XPXPXPXPXPObject Literals
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 19
• The general syntax to create a custom object
as an object literal
var objName = {
name1: value1,
name2: value2,
…
};
where
– objName is the name of the object
XPXPXPXPXPObject Literals (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 20
– name1, name2, and so on are the names
associated with that object
– value1, value2, and so on are the values
assigned to those names
• Each name:value pair contains a property and
property value associated with the object
XPXPXPXPXPObject Literals (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 21
XPXPXPXPXPDot Operators and Bracket Notation
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 22
• Dot operator: Connects the object name with
an object property
object.property
• Object properties can also be written using the
bracket notation
object[“property”]
where object is the object name and
property is the object property
XPXPXPXPXP
Dot Operators and Bracket Notation
(continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 23
• Example: The value of the currentBank
property of the pokerGame object could be set
with either
pokerGame.currentBank = 500;
or
pokerGame[“currentBank”] = 500;
XPXPXPXPXP
Dot Operators and Bracket Notation
(continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 24
XPXPXPXPXPCreating a Custom Method
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 25
• Methods are added to a custom object by
including a function name and its commands
as the following name:value pair:
var objName = {
method: function() {
commands
}
}
where method is the name of the method and
commands are commands run by the method
XPXPXPXPXPCreating a Custom Method (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 26
• Example: The following code adds the
placeBet() method to the pokerDeck object:
var pokerDeck = {
currentBank: null,
currentBet: null,
placeBet: function() {
this.currentBank -= this.currentBet;
return currentBank;
}
}
XPXPXPXPXPCreating a Custom Method (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 27
• The placeBet() method uses the this keyword
to reference the current object
• The -= assignment operator is used to subtract
the value of the current bet from the current
bank value
• The method concludes by returning the value
of the currentBank property
XPXPXPXPXPCreating a Custom Method (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 28
• To apply the placeBet() method to the
pokerDeck object, run the following
expression:
pokerDeck.placeBet()
• Add placeBet() method to the pokerGame
object to reduce the bank value by the size of
the bet
• The placeBet() method should be run
whenever the user clicks the Deal button
XPXPXPXPXPCreating a Custom Method (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 29
XPXPXPXPXPCreating an Object with the new Operator
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 30
• Syntax to create a custom object with the
new operator
var objName = new Object();
object.property = value;
object.method = function() {
commands
};
where objName is the object name, property
is a property defined for that object, and
method is a method assigned to that object
XPXPXPXPXP
Creating an Object with the new Operator
(continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 31
• The new Object() statement is equivalent to
an empty object literal {} that creates an
object devoid of properties and methods
• The biggest limitation of an object created
either as an object literal or with the new
Object() command is that the object is not
reusable
XPXPXPXPXPConstructor Functions
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 32
• Object class can be created using an object
constructor or a constructor that defines the
properties and methods associated with the
object type
• Object instance or instance: Specific object
that is based on an object class
• Creating the object based on an object class is
known as instantiating an object
XPXPXPXPXPConstructor Functions (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 33
• Object constructors are defined with the
following constructor function:
function objClass(parameters) {
this.prop1 = value1;
this.prop2 = value2;…
this.method1 = function1;
this.method2 = function2;…
}
where
– objClass is the name of the object class
XPXPXPXPXPConstructor Functions (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 34
– parameters are the parameters used by the
constructor function
– prop1, prop2, and so on are the properties
associated with that object class
– method1, method2, and so on are the methods
associated with that object class
– this keyword refers to any object instance of the
particular object class
XPXPXPXPXPConstructor Functions (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 35
• Instances of an object are created with the
following command:
var objInstance = new
objClass(parameters);
where
– objInstance is a specific instance of the object
– objClass is the object class as defined by the
constructor function
– parameters are the values of any parameters
included in the constructor function
XPXPXPXPXPConstructor Functions (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 36
XPXPXPXPXPCombining Object Classes
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 37
• One object class can include objects defined in
other classes
XPXPXPXPXPCombining Object Classes (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 38
• To instantiate an object from the pokerDeck
class, create a variable named “myDeck” using
the following command:
var myDeck = new pokerDeck();
• The array of pokerCard objects for the myDeck
variable is referenced with the following
expression:
myDeck.cards
XPXPXPXPXPCombining Object Classes (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 39
• Each card in the deck can be retrieved by
referencing an index in the cards array
• Example: The following expression retrieves
the fourth card from the deck:
myDeck.cards[4]
XPXPXPXPXPCombining Object Classes (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 40
• All card games require the deck of cards to be
randomly sorted
• The sort() method of Array objects can be
used for random arrangement of the array
items
• Add the shuffle() method to the pokerDeck
object class to randomize the order of items in
the cards array
XPXPXPXPXPCombining Object Classes (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 41
XPXPXPXPXPWorking with Object Prototypes
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 42
• Every object has a prototype, which is a
template for all the properties and methods
associated with the object’s class
• When an object is instantiated from a
constructor function, it copies the properties
and methods from the prototype into the new
object
XPXPXPXPXPDefining a Prototype Method
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 43
• The prototype is itself an object and is
referenced using the following expression:
objName.prototype
where objName is the name of the object class
• Example: The prototype for the pokerCard
class of objects is referenced as follows:
pokerCard.prototype
XPXPXPXPXP
Defining a Prototype Method
(continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 44
• Apply the following command to add a
method to a prototype:
objName.prototype.method = function;
where method is the name of the method and
function is the function applied by the
method
XPXPXPXPXP
Defining a Prototype Method
(continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 45
XPXPXPXPXP
Defining a Prototype Method
(continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 46
XPXPXPXPXPCombining Objects
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 47
• Any object class can inherit the properties and
methods from another class using prototypes
• The hierarchy of object classes creates a
prototype chain ranging from superclass to
subclasses
• Superclass: Base object class in a prototype
chain
• Subclasses: Lower classes in a prototype chain
XPXPXPXPXPCombining Objects (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 48
• Prototypal inheritance: Process by which the
properties and methods of superclasses are
shared with the subclasses
XPXPXPXPXPCreating a Prototype Chain
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 49
• A prototype chain is created by defining an
object prototype as an instance of an object
class
• Order of classes is important while defining
the prototype chain
• Start at the top of the hierarchy, and move
down to the lower subclasses
XPXPXPXPXP
Creating a Prototype Chain
(continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 50
• JavaScript resolves the code that references an
object property or method in the following
order:
– Check for the property or method within the
current object instance
– Check for the property or method with the
object’s prototype
XPXPXPXPXP
Creating a Prototype Chain
(continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 51
– If the prototype is an instance of another object,
check for the property or method in that object
– Continue moving down the chain until the
property or method is located or the end of the
chain is reached
• All prototype chains ultimately find their
source in the base object
XPXPXPXPXPThe Base Object
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 52
• Base object or Object: Fundamental
JavaScript object whose methods are available
to all objects
• A subclass of the base object is created when a
custom object is created using an object literal
or by applying the new Object() command
XPXPXPXPXPThe Base Object (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 53
XPXPXPXPXPThe Base Object (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 54
• Example: To determine whether an object
supports a particular property use the
hasOwnProperty() method as follows:
pokerCard.hasOwnProperty(“rank”);
• The code returns true if the pokerCard object
supports the rank property
XPXPXPXPXPThe Base Object (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 55
• The constructor for Object also supports
methods that can be used to retrieve and
define properties for any object
XPXPXPXPXP
Using the apply() and call()
Methods
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 56
• Apply or call a method from one object for use
in another object in order to share methods
between objects
• Borrow a method from one object class using
the following apply() method:
function.apply(thisObj [,argArray])
XPXPXPXPXP
Using the apply() and call()
Methods (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 57
where
– function is a reference to a function
– thisObj is the object that receives the actions of
the function
– argArray is an optional array of argument values
sent to the function
XPXPXPXPXP
Using the apply() and call()
Methods (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 58
• The call() method is similar to the apply()
method except that the argument values are
placed in a comma-separated list of values
instead of an array
• The syntax of the call() method
function.call(thisObj, arg1, arg2,
arg3, ...)
where arg1, arg2, arg3, and so on is the
comma-separated list of argument values for
function
XPXPXPXPXPCombining Objects and Arrays
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 59
• A custom object contains data stored in arrays
• JavaScript’s built-in Array object methods
speed up the efficiency of a code by looping
through the contents of an array
XPXPXPXPXPApplying the every() Array method
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 60
• Use the every() method to test whether
every item in an array matches a specified
condition
• Example: Use every() method to test whether
every card in the cards array of the pokerHand
object has the same value for the suit property
XPXPXPXPXP
Applying the every() Array method
(continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 61
XPXPXPXPXP
Creating an Object Literal with the
forEach() Method
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 62
• forEach() method is used to run a command
block for each item in an array
XPXPXPXPXPApplying a for…in loop
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 63
• for…in loop is used to examine the properties
and keys of an object as follows:
for (prop in obj) {
commands
}
where prop references the properties
contained within the obj object
XPXPXPXPXPApplying a for…in loop (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 64
• Example: Loop through the contents of the
following employee object:
var employee = {
name: “Robert Voiklund”,
position: “manager”,
email: “rvoiklund@example.com” };
• Solution: Apply the following for … in loop:
for (prop in employee) {
console.info(prop + “ is “ +
employee[prop]); }
XPXPXPXPXPApplying a for…in loop (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 65
• for…in loops do not follow a specific order
because properties can be listed and read out
in any order
• Only the properties that are countable or
enumerable are accessible to for…in loops
XPXPXPXPXPApplying a for…in loop (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 66
• Determine whether a property is enumerable
using the propertyIsEnumerable() method
obj.propertyIsEnumerable(prop)
where obj is the object and prop is the
property
• Use a for…in loop to loop through every
property in the object
XPXPXPXPXPApplying a for…in loop (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 67

More Related Content

What's hot

Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSDr. Ahmed Al Zaidy
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsDr. Ahmed Al Zaidy
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaDr. Ahmed Al Zaidy
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsDr. Ahmed Al Zaidy
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptDr. Ahmed Al Zaidy
 
Moving from SQL Server to MongoDB
Moving from SQL Server to MongoDBMoving from SQL Server to MongoDB
Moving from SQL Server to MongoDBNick Court
 
Combine Spring Data Neo4j and Spring Boot to quickl
Combine Spring Data Neo4j and Spring Boot to quicklCombine Spring Data Neo4j and Spring Boot to quickl
Combine Spring Data Neo4j and Spring Boot to quicklNeo4j
 
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...Neo4j
 
Webinar: From Relational Databases to MongoDB - What You Need to Know
Webinar: From Relational Databases to MongoDB - What You Need to KnowWebinar: From Relational Databases to MongoDB - What You Need to Know
Webinar: From Relational Databases to MongoDB - What You Need to KnowMongoDB
 
OrientDB vs Neo4j - and an introduction to NoSQL databases
OrientDB vs Neo4j - and an introduction to NoSQL databasesOrientDB vs Neo4j - and an introduction to NoSQL databases
OrientDB vs Neo4j - and an introduction to NoSQL databasesCurtis Mosters
 
Neo4j + Tableau Visual Analytics - GraphConnect SF 2015
Neo4j + Tableau Visual Analytics - GraphConnect SF 2015 Neo4j + Tableau Visual Analytics - GraphConnect SF 2015
Neo4j + Tableau Visual Analytics - GraphConnect SF 2015 Neo4j
 
Generalized framework for using NoSQL Databases
Generalized framework for using NoSQL DatabasesGeneralized framework for using NoSQL Databases
Generalized framework for using NoSQL DatabasesKIRAN V
 
SDEC2011 NoSQL Data modelling
SDEC2011 NoSQL Data modellingSDEC2011 NoSQL Data modelling
SDEC2011 NoSQL Data modellingKorea Sdec
 
XML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataXML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataMarco Gralike
 
Migrating from RDBMS to MongoDB
Migrating from RDBMS to MongoDBMigrating from RDBMS to MongoDB
Migrating from RDBMS to MongoDBMongoDB
 
Advanced Schema Design Patterns
Advanced Schema Design PatternsAdvanced Schema Design Patterns
Advanced Schema Design PatternsMongoDB
 
EclipseCon 2021 NoSQL Endgame
EclipseCon 2021 NoSQL EndgameEclipseCon 2021 NoSQL Endgame
EclipseCon 2021 NoSQL EndgameThodoris Bais
 
NoSQL on ACID - Meet Unstructured Postgres
NoSQL on ACID - Meet Unstructured PostgresNoSQL on ACID - Meet Unstructured Postgres
NoSQL on ACID - Meet Unstructured PostgresEDB
 
MTA animations graphics_accessing data
MTA animations graphics_accessing dataMTA animations graphics_accessing data
MTA animations graphics_accessing dataDhairya Joshi
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchEelco Visser
 

What's hot (20)

Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSS
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statements
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimedia
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and Columns
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScript
 
Moving from SQL Server to MongoDB
Moving from SQL Server to MongoDBMoving from SQL Server to MongoDB
Moving from SQL Server to MongoDB
 
Combine Spring Data Neo4j and Spring Boot to quickl
Combine Spring Data Neo4j and Spring Boot to quicklCombine Spring Data Neo4j and Spring Boot to quickl
Combine Spring Data Neo4j and Spring Boot to quickl
 
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
 
Webinar: From Relational Databases to MongoDB - What You Need to Know
Webinar: From Relational Databases to MongoDB - What You Need to KnowWebinar: From Relational Databases to MongoDB - What You Need to Know
Webinar: From Relational Databases to MongoDB - What You Need to Know
 
OrientDB vs Neo4j - and an introduction to NoSQL databases
OrientDB vs Neo4j - and an introduction to NoSQL databasesOrientDB vs Neo4j - and an introduction to NoSQL databases
OrientDB vs Neo4j - and an introduction to NoSQL databases
 
Neo4j + Tableau Visual Analytics - GraphConnect SF 2015
Neo4j + Tableau Visual Analytics - GraphConnect SF 2015 Neo4j + Tableau Visual Analytics - GraphConnect SF 2015
Neo4j + Tableau Visual Analytics - GraphConnect SF 2015
 
Generalized framework for using NoSQL Databases
Generalized framework for using NoSQL DatabasesGeneralized framework for using NoSQL Databases
Generalized framework for using NoSQL Databases
 
SDEC2011 NoSQL Data modelling
SDEC2011 NoSQL Data modellingSDEC2011 NoSQL Data modelling
SDEC2011 NoSQL Data modelling
 
XML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataXML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured data
 
Migrating from RDBMS to MongoDB
Migrating from RDBMS to MongoDBMigrating from RDBMS to MongoDB
Migrating from RDBMS to MongoDB
 
Advanced Schema Design Patterns
Advanced Schema Design PatternsAdvanced Schema Design Patterns
Advanced Schema Design Patterns
 
EclipseCon 2021 NoSQL Endgame
EclipseCon 2021 NoSQL EndgameEclipseCon 2021 NoSQL Endgame
EclipseCon 2021 NoSQL Endgame
 
NoSQL on ACID - Meet Unstructured Postgres
NoSQL on ACID - Meet Unstructured PostgresNoSQL on ACID - Meet Unstructured Postgres
NoSQL on ACID - Meet Unstructured Postgres
 
MTA animations graphics_accessing data
MTA animations graphics_accessing dataMTA animations graphics_accessing data
MTA animations graphics_accessing data
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language Workbench
 

Similar to Chapter 14 Exploring Object-based Programming

9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdf9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdfHaboonMohmed
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxMegha V
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOMJalpesh Vasa
 
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」techtalkdwango
 
第7回みゆっき☆Think 本気で学ぶ JavaScript
第7回みゆっき☆Think 本気で学ぶ JavaScript第7回みゆっき☆Think 本気で学ぶ JavaScript
第7回みゆっき☆Think 本気で学ぶ JavaScriptTakuya Fujimura
 
NAVTechDays 2017 Json Meets NAV
NAVTechDays 2017 Json Meets NAVNAVTechDays 2017 Json Meets NAV
NAVTechDays 2017 Json Meets NAVGunnar Gestsson
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)Eduard Tomàs
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016Codemotion
 
W3C HTML5 KIG-How to write low garbage real-time javascript
W3C HTML5 KIG-How to write low garbage real-time javascriptW3C HTML5 KIG-How to write low garbage real-time javascript
W3C HTML5 KIG-How to write low garbage real-time javascriptChanghwan Yi
 

Similar to Chapter 14 Exploring Object-based Programming (20)

html5 tutorial.pdf
html5 tutorial.pdfhtml5 tutorial.pdf
html5 tutorial.pdf
 
9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdf9781305503922t5-200824144338.pdf
9781305503922t5-200824144338.pdf
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
 
WEB222-lecture-4.pptx
WEB222-lecture-4.pptxWEB222-lecture-4.pptx
WEB222-lecture-4.pptx
 
Javascript
JavascriptJavascript
Javascript
 
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
 
第7回みゆっき☆Think 本気で学ぶ JavaScript
第7回みゆっき☆Think 本気で学ぶ JavaScript第7回みゆっき☆Think 本気で学ぶ JavaScript
第7回みゆっき☆Think 本気で学ぶ JavaScript
 
Ddpz2613 topic9 java
Ddpz2613 topic9 javaDdpz2613 topic9 java
Ddpz2613 topic9 java
 
Java scriptforjavadev part2a
Java scriptforjavadev part2aJava scriptforjavadev part2a
Java scriptforjavadev part2a
 
NAVTechDays 2017 Json Meets NAV
NAVTechDays 2017 Json Meets NAVNAVTechDays 2017 Json Meets NAV
NAVTechDays 2017 Json Meets NAV
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
W3C HTML5 KIG-How to write low garbage real-time javascript
W3C HTML5 KIG-How to write low garbage real-time javascriptW3C HTML5 KIG-How to write low garbage real-time javascript
W3C HTML5 KIG-How to write low garbage real-time javascript
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 

More from Dr. Ahmed Al Zaidy

testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2Dr. Ahmed Al Zaidy
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityDr. Ahmed Al Zaidy
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementDr. Ahmed Al Zaidy
 
Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Dr. Ahmed Al Zaidy
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application SecurityDr. Ahmed Al Zaidy
 
Chapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityChapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityDr. Ahmed Al Zaidy
 
Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Dr. Ahmed Al Zaidy
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyDr. Ahmed Al Zaidy
 
Chapter 5 Networking and Server Attacks
Chapter 5 Networking and Server AttacksChapter 5 Networking and Server Attacks
Chapter 5 Networking and Server AttacksDr. Ahmed Al Zaidy
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IDr. Ahmed Al Zaidy
 
Chapter 2 Malware and Social Engineering Attacks
Chapter 2 Malware and Social Engineering AttacksChapter 2 Malware and Social Engineering Attacks
Chapter 2 Malware and Social Engineering AttacksDr. Ahmed Al Zaidy
 

More from Dr. Ahmed Al Zaidy (17)

Integer overflows
Integer overflowsInteger overflows
Integer overflows
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2
 
Fundamental of testing
Fundamental of testingFundamental of testing
Fundamental of testing
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access Management
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
 
Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security Chapter 10 Mobile and Embedded Device Security
Chapter 10 Mobile and Embedded Device Security
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application Security
 
Chapter 8 Wireless Network Security
Chapter 8 Wireless Network SecurityChapter 8 Wireless Network Security
Chapter 8 Wireless Network Security
 
Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and Technology
 
Chapter 5 Networking and Server Attacks
Chapter 5 Networking and Server AttacksChapter 5 Networking and Server Attacks
Chapter 5 Networking and Server Attacks
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K I
 
Chapter 3 Basic Cryptography
Chapter 3 Basic CryptographyChapter 3 Basic Cryptography
Chapter 3 Basic Cryptography
 
Chapter 2 Malware and Social Engineering Attacks
Chapter 2 Malware and Social Engineering AttacksChapter 2 Malware and Social Engineering Attacks
Chapter 2 Malware and Social Engineering Attacks
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

Chapter 14 Exploring Object-based Programming

  • 1. HTML5, CSS3, and JavaScript 6th Edition Exploring Object-Based Programming Tutorial 14 Carey
  • 2. XPXPXPXPXPObjectives • Use nested functions • Create an object literal • Define object properties and methods • Define an object class • Use object constructor functions • Instantiating an object New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 2
  • 3. XPXPXPXPXPObjectives (continued) • Define an object prototype • Explore prototype chains • Use the apply() and call() methods • Work with objects and arrays • Create a for…in loop New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 3
  • 4. XPXPXPXPXPWorking with Nested Functions • Any function, including named functions, can be nested within another function as follows: function outsideFn() { commands function insideFn() { commands } commands } where – outsideFn() is the outer or containing function New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 4
  • 5. XPXPXPXPXP Working with Nested Functions (continued 1) – insideFn() is the inner or nested function • Scope of a nested function is limited to the commands within the containing function • Nested function is hidden from other code in the script, making the code contained and easier to manage New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 5
  • 6. XPXPXPXPXP Working with Nested Functions (continued 2) • Example: Nested functions can be used in developing the poker game app for Arthur’s Games website • The app displays the poker table on which users will play a game of draw poker New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 6
  • 7. XPXPXPXPXP Working with Nested Functions (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 7
  • 8. XPXPXPXPXP Working with Nested Functions (continued 4) • All operations for the game will be stored within the playDrawPoker() function • playDrawPoker() function runs automatically when the page loads • playDrawPoker() function is created by adding references to all the buttons on the poker table New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 8
  • 9. XPXPXPXPXP Working with Nested Functions (continued 5) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 9
  • 10. XPXPXPXPXP Working with Nested Functions (continued 6) • Elements on the Draw Poker page perform the following tasks: – Deal button deals five cards from the poker deck into the player’s hand – Draw button replaces all selected cards in the player’s hand with new cards from the deck New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 10
  • 11. XPXPXPXPXP Working with Nested Functions (continued 7) – Stand button signals to the dealer that the player wants to keep all the cards in the dealt hand – Reset button restarts the game with a fresh pot, resetting the bank value to $500 – Bet selection list places the bet before the next hand is dealt New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 11
  • 12. XPXPXPXPXP Working with Nested Functions (continued 8) • The Deal, Draw, and Stand buttons and the Bet selection list will be turned on and off depending on the state of the game • The Draw and Stand buttons are disabled before the deal • The Deal button and Bet selection list are disabled while the current hand is in play New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 12
  • 13. XPXPXPXPXP Working with Nested Functions (continued 9) • To disable and enable the buttons, nest the required functions within the playDrawPoker() function • The functions for disabling or enabling the selected object also set the opacity style of the selected object • Disabled objects are semi-transparent on the page New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 13
  • 14. XPXPXPXPXP Working with Nested Functions (continued 10) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 14
  • 15. XPXPXPXPXP Working with Nested Functions (continued 11) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 15
  • 16. XPXPXPXPXPIntroducing Custom Objects New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 16 • There are three kinds of JavaScript objects – Native objects, such as Date or Array objects, are part of the JavaScript language – Host objects are objects provided by the browser for use in interacting with the web document and browser, such as the window, document, or form objects – Custom objects, also known as user-defined objects, are objects created by the user for specific programming tasks
  • 17. XPXPXPXPXPIntroducing Custom Objects (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 17 • The following custom objects are created for the poker game application: – A poker game object that contains information about the card game being played – A poker deck object that contains information about the cards used in the game – A poker hand object that contains information about the hand played by the user in the game – Poker card objects that contain information about the individual cards in the poker hand
  • 18. XPXPXPXPXPIntroducing Custom Objects (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 18 • A custom object can be defined in one of the following three ways: – Creating it as an object literal – Using an object constructor – Applying the object create() method
  • 19. XPXPXPXPXPObject Literals New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 19 • The general syntax to create a custom object as an object literal var objName = { name1: value1, name2: value2, … }; where – objName is the name of the object
  • 20. XPXPXPXPXPObject Literals (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 20 – name1, name2, and so on are the names associated with that object – value1, value2, and so on are the values assigned to those names • Each name:value pair contains a property and property value associated with the object
  • 21. XPXPXPXPXPObject Literals (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 21
  • 22. XPXPXPXPXPDot Operators and Bracket Notation New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 22 • Dot operator: Connects the object name with an object property object.property • Object properties can also be written using the bracket notation object[“property”] where object is the object name and property is the object property
  • 23. XPXPXPXPXP Dot Operators and Bracket Notation (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 23 • Example: The value of the currentBank property of the pokerGame object could be set with either pokerGame.currentBank = 500; or pokerGame[“currentBank”] = 500;
  • 24. XPXPXPXPXP Dot Operators and Bracket Notation (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 24
  • 25. XPXPXPXPXPCreating a Custom Method New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 25 • Methods are added to a custom object by including a function name and its commands as the following name:value pair: var objName = { method: function() { commands } } where method is the name of the method and commands are commands run by the method
  • 26. XPXPXPXPXPCreating a Custom Method (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 26 • Example: The following code adds the placeBet() method to the pokerDeck object: var pokerDeck = { currentBank: null, currentBet: null, placeBet: function() { this.currentBank -= this.currentBet; return currentBank; } }
  • 27. XPXPXPXPXPCreating a Custom Method (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 27 • The placeBet() method uses the this keyword to reference the current object • The -= assignment operator is used to subtract the value of the current bet from the current bank value • The method concludes by returning the value of the currentBank property
  • 28. XPXPXPXPXPCreating a Custom Method (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 28 • To apply the placeBet() method to the pokerDeck object, run the following expression: pokerDeck.placeBet() • Add placeBet() method to the pokerGame object to reduce the bank value by the size of the bet • The placeBet() method should be run whenever the user clicks the Deal button
  • 29. XPXPXPXPXPCreating a Custom Method (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 29
  • 30. XPXPXPXPXPCreating an Object with the new Operator New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 30 • Syntax to create a custom object with the new operator var objName = new Object(); object.property = value; object.method = function() { commands }; where objName is the object name, property is a property defined for that object, and method is a method assigned to that object
  • 31. XPXPXPXPXP Creating an Object with the new Operator (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 31 • The new Object() statement is equivalent to an empty object literal {} that creates an object devoid of properties and methods • The biggest limitation of an object created either as an object literal or with the new Object() command is that the object is not reusable
  • 32. XPXPXPXPXPConstructor Functions New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 32 • Object class can be created using an object constructor or a constructor that defines the properties and methods associated with the object type • Object instance or instance: Specific object that is based on an object class • Creating the object based on an object class is known as instantiating an object
  • 33. XPXPXPXPXPConstructor Functions (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 33 • Object constructors are defined with the following constructor function: function objClass(parameters) { this.prop1 = value1; this.prop2 = value2;… this.method1 = function1; this.method2 = function2;… } where – objClass is the name of the object class
  • 34. XPXPXPXPXPConstructor Functions (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 34 – parameters are the parameters used by the constructor function – prop1, prop2, and so on are the properties associated with that object class – method1, method2, and so on are the methods associated with that object class – this keyword refers to any object instance of the particular object class
  • 35. XPXPXPXPXPConstructor Functions (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 35 • Instances of an object are created with the following command: var objInstance = new objClass(parameters); where – objInstance is a specific instance of the object – objClass is the object class as defined by the constructor function – parameters are the values of any parameters included in the constructor function
  • 36. XPXPXPXPXPConstructor Functions (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 36
  • 37. XPXPXPXPXPCombining Object Classes New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 37 • One object class can include objects defined in other classes
  • 38. XPXPXPXPXPCombining Object Classes (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 38 • To instantiate an object from the pokerDeck class, create a variable named “myDeck” using the following command: var myDeck = new pokerDeck(); • The array of pokerCard objects for the myDeck variable is referenced with the following expression: myDeck.cards
  • 39. XPXPXPXPXPCombining Object Classes (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 39 • Each card in the deck can be retrieved by referencing an index in the cards array • Example: The following expression retrieves the fourth card from the deck: myDeck.cards[4]
  • 40. XPXPXPXPXPCombining Object Classes (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 40 • All card games require the deck of cards to be randomly sorted • The sort() method of Array objects can be used for random arrangement of the array items • Add the shuffle() method to the pokerDeck object class to randomize the order of items in the cards array
  • 41. XPXPXPXPXPCombining Object Classes (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 41
  • 42. XPXPXPXPXPWorking with Object Prototypes New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 42 • Every object has a prototype, which is a template for all the properties and methods associated with the object’s class • When an object is instantiated from a constructor function, it copies the properties and methods from the prototype into the new object
  • 43. XPXPXPXPXPDefining a Prototype Method New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 43 • The prototype is itself an object and is referenced using the following expression: objName.prototype where objName is the name of the object class • Example: The prototype for the pokerCard class of objects is referenced as follows: pokerCard.prototype
  • 44. XPXPXPXPXP Defining a Prototype Method (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 44 • Apply the following command to add a method to a prototype: objName.prototype.method = function; where method is the name of the method and function is the function applied by the method
  • 45. XPXPXPXPXP Defining a Prototype Method (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 45
  • 46. XPXPXPXPXP Defining a Prototype Method (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 46
  • 47. XPXPXPXPXPCombining Objects New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 47 • Any object class can inherit the properties and methods from another class using prototypes • The hierarchy of object classes creates a prototype chain ranging from superclass to subclasses • Superclass: Base object class in a prototype chain • Subclasses: Lower classes in a prototype chain
  • 48. XPXPXPXPXPCombining Objects (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 48 • Prototypal inheritance: Process by which the properties and methods of superclasses are shared with the subclasses
  • 49. XPXPXPXPXPCreating a Prototype Chain New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 49 • A prototype chain is created by defining an object prototype as an instance of an object class • Order of classes is important while defining the prototype chain • Start at the top of the hierarchy, and move down to the lower subclasses
  • 50. XPXPXPXPXP Creating a Prototype Chain (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 50 • JavaScript resolves the code that references an object property or method in the following order: – Check for the property or method within the current object instance – Check for the property or method with the object’s prototype
  • 51. XPXPXPXPXP Creating a Prototype Chain (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 51 – If the prototype is an instance of another object, check for the property or method in that object – Continue moving down the chain until the property or method is located or the end of the chain is reached • All prototype chains ultimately find their source in the base object
  • 52. XPXPXPXPXPThe Base Object New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 52 • Base object or Object: Fundamental JavaScript object whose methods are available to all objects • A subclass of the base object is created when a custom object is created using an object literal or by applying the new Object() command
  • 53. XPXPXPXPXPThe Base Object (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 53
  • 54. XPXPXPXPXPThe Base Object (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 54 • Example: To determine whether an object supports a particular property use the hasOwnProperty() method as follows: pokerCard.hasOwnProperty(“rank”); • The code returns true if the pokerCard object supports the rank property
  • 55. XPXPXPXPXPThe Base Object (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 55 • The constructor for Object also supports methods that can be used to retrieve and define properties for any object
  • 56. XPXPXPXPXP Using the apply() and call() Methods New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 56 • Apply or call a method from one object for use in another object in order to share methods between objects • Borrow a method from one object class using the following apply() method: function.apply(thisObj [,argArray])
  • 57. XPXPXPXPXP Using the apply() and call() Methods (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 57 where – function is a reference to a function – thisObj is the object that receives the actions of the function – argArray is an optional array of argument values sent to the function
  • 58. XPXPXPXPXP Using the apply() and call() Methods (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 58 • The call() method is similar to the apply() method except that the argument values are placed in a comma-separated list of values instead of an array • The syntax of the call() method function.call(thisObj, arg1, arg2, arg3, ...) where arg1, arg2, arg3, and so on is the comma-separated list of argument values for function
  • 59. XPXPXPXPXPCombining Objects and Arrays New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 59 • A custom object contains data stored in arrays • JavaScript’s built-in Array object methods speed up the efficiency of a code by looping through the contents of an array
  • 60. XPXPXPXPXPApplying the every() Array method New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 60 • Use the every() method to test whether every item in an array matches a specified condition • Example: Use every() method to test whether every card in the cards array of the pokerHand object has the same value for the suit property
  • 61. XPXPXPXPXP Applying the every() Array method (continued) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 61
  • 62. XPXPXPXPXP Creating an Object Literal with the forEach() Method New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 62 • forEach() method is used to run a command block for each item in an array
  • 63. XPXPXPXPXPApplying a for…in loop New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 63 • for…in loop is used to examine the properties and keys of an object as follows: for (prop in obj) { commands } where prop references the properties contained within the obj object
  • 64. XPXPXPXPXPApplying a for…in loop (continued 1) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 64 • Example: Loop through the contents of the following employee object: var employee = { name: “Robert Voiklund”, position: “manager”, email: “rvoiklund@example.com” }; • Solution: Apply the following for … in loop: for (prop in employee) { console.info(prop + “ is “ + employee[prop]); }
  • 65. XPXPXPXPXPApplying a for…in loop (continued 2) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 65 • for…in loops do not follow a specific order because properties can be listed and read out in any order • Only the properties that are countable or enumerable are accessible to for…in loops
  • 66. XPXPXPXPXPApplying a for…in loop (continued 3) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 66 • Determine whether a property is enumerable using the propertyIsEnumerable() method obj.propertyIsEnumerable(prop) where obj is the object and prop is the property • Use a for…in loop to loop through every property in the object
  • 67. XPXPXPXPXPApplying a for…in loop (continued 4) New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition 67