SlideShare a Scribd company logo
1 of 21
Object Oriented
Programming in JavaScript
Dr. Charles Severance
www.wa4e.com
http://www.wa4e.com/code/javascript-objects
http://www.wa4e.com/code/javascript-objects.zip
Definitions
• Class - a template - Dog
• Method or Message - A defined capability of a class -
bark()
• Attribute - A defined data item in a class - color
• Object or Instance - A particular instance of a class -
Lassie
Image CC-By 2.0: https://www.flickr.com/photos/dinnerseries/23570475099
Terminology: Class
Defines the abstract characteristics of a thing (object), including
the thing's characteristics (its attributes, fields, or properties) and
the thing’s behaviors (the things it can do, or methods, operations,
or features). One might say that a class is a blueprint or factory
that describes the nature of something. For example, the class Dog
would consist of traits shared by all dogs, such as breed and fur
color (characteristics), and the ability to bark and sit (behaviors).
http://en.wikipedia.org/wiki/Object-
oriented_programming
Terminology: Class
http://en.wikipedia.org/wiki/Object-
oriented_programming
A pattern (exemplar) of a class. The class
of Dog defines all possible dogs by listing
the characteristics and behaviors they can
have; the object Lassie is one particular
dog, with particular versions of the
characteristics. A Dog has fur; Lassie has
brown-and-white fur.
Terminology: Instance
One can have an instance of a class or a particular object. The
instance is the actual object created at runtime. In programmer
jargon, the Lassie object is an instance of the Dog class. The set of
values of the attributes of a particular object is called its state. The
object consists of state and the behavior that’s defined in the
object’s class.
Object and Instance are often used
interchangeably.
http://en.wikipedia.org/wiki/Object-
oriented_programming
Terminology: Method
An object’s abilities. In language, methods are verbs. Lassie, being
a Dog, has the ability to bark. So bark() is one of Lassie’s methods.
She may have other methods as well, for example sit() or eat() or
walk() or save_timmy(). Within the program, using a method
usually affects only one particular object; all Dogs can bark, but
you need only one particular dog to do the barking
Method and Message are often used
interchangeably.
http://en.wikipedia.org/wiki/Object-
oriented_programming
Objects in JavaScript
• The OO pattern in JavaScript is a little different.
• The function is indeed a store and reuse pattern.
• The function keyword returns a value which is the
function itself - it makes a function!
First-Class Functions
http://en.wikipedia.org/wiki/First-class_function
In computer science, a programming language is said to
have first-class functions if it treats functions as first-
class citizens. Specifically, this means the language
supports passing functions as arguments to other
functions, returning them as the values from other
functions, and assigning them to variables or storing
them in data structures.
A Sample Class
function PartyAnimal() {
this.x = 0;
this.party = function () {
this.x = this.x + 1;
console.log("So far "+this.x);
}
}
an = new PartyAnimal();
an.party();
an.party();
an.party();
This is the template for
making PartyAnimal
objects.
Each PartyAnimal
object has a bit of
data.
Each PartyAnimal object
has a bit of code.
Create a PartyAnimal
object
Tell the object to run
the party() code. js01.ht
m
an
x:
party()
function PartyAnimal() {
this.x = 0;
this.party = function () {
this.x = this.x + 1;
console.log("So far "+this.x);
}
}
an = new PartyAnimal();
an.party();
an.party();
an.party();
js-
01.htm
Object Life Cycle
http://en.wikipedia.org/wiki/Constructor_(computer_science)
Object Life Cycle
• Objects are created, used, and discarded
• Constructors are implicit in JavaScript - natural
• A constructor in a class is a special block of
statements called when an object is created
• Destructors are not provided by JavaScript
http://en.wikipedia.org/wiki/Constructor_(computer_scie
nce)
js03.ht
m
function PartyAnimal() {
this.x = 0;
console.log("In the 'constructor'");
this.party = function () {
this.x = this.x + 1;
console.log("So far "+this.x);
}
}
an = new PartyAnimal();
an.party();
an.party();
an.party();
Many Instances
• We can create lots of objects - the class is the template
for the object.
• We can store each distinct object in its own variable.
• We call this having multiple instances of the same class.
• Each instance has its own copy of the instance variables.
function PartyAnimal(nam) {
this.x = 0;
this.name = nam;
console.log("Built "+nam);
this.party = function () {
this.x = this.x + 1;
console.log(nam+"="+this.x);
}
}
s = new PartyAnimal("Sally");
s.party();
j = new PartyAnimal("Jim");
j.party();
s.party();
Constructors can have
additional parameters.
These can be used to set up
instance variables for the
particular instance of the
class (i.e., for the particular
object).
js04.ht
m
s
x:
name:
j
x:
name:
function PartyAnimal(nam) {
this.x = 0;
this.name = nam;
console.log("Built "+nam);
this.party = function () {
this.x = this.x + 1;
console.log(nam+"="+this.x);
}
}
s = new PartyAnimal("Sally");
s.party();
j = new PartyAnimal("Jim");
j.party();
s.party();
js04.ht
m
Definitions
• Class - a template - Dog
• Method or Message - A defined capability of a class -
bark()
• Object or Instance - A particular instance of a class -
Lassie
• Constructor - A method which is called when the
instance / object is created
Summary
• The key for this lecture is to understand how to read
OO documentation for JavaScript and how to use
objects.
• Building brand new complex objects is more
advanced.
• It is important to remember that JavaScript uses
objects as its “Associative Array”.
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) as part of www.wa4e.com and made
available under a Creative Commons Attribution 4.0 License.
Please maintain this last slide in all copies of the document to
comply with the attribution requirements of the license. If you
make a change, feel free to add your name and organization
to the list of contributors on this page as you republish the
materials.
Initial Development: Charles Severance, University of
Michigan School of Information
Insert new Contributors and Translators here including names
and dates
Continue new Contributors and Translators here
Additional Source Information
• “Snowman Cookie Cutter” by Didriks is licensed under CC BY
https://www.flickr.com/photos/dinnerseries/23570475099
• Photo from the television program Lassie. Lassie watches as Jeff (Tommy Rettig) works on his bike is Public Domain
https://en.wikipedia.org/wiki/Lassie - /media/File:Lassie_and_Tommy_Rettig_1956.JPG

More Related Content

Similar to JS-02-JavaScript-Objects.ppt

Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Using class and object java
Using class and object javaUsing class and object java
Using class and object javamha4
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objectsmaznabili
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptmuneshwarbisen1
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfArpitaJana28
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS ImplimentationUsman Mehmood
 
Object in python tells about object oriented programming in python
Object in python tells about object oriented programming in pythonObject in python tells about object oriented programming in python
Object in python tells about object oriented programming in pythonReshmiShaw2
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingManish Pandit
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
com-213-unified-modelling-launguage-programming-theory.pdf
com-213-unified-modelling-launguage-programming-theory.pdfcom-213-unified-modelling-launguage-programming-theory.pdf
com-213-unified-modelling-launguage-programming-theory.pdfAhmadInternetCafe
 
Java for android developers
Java for android developersJava for android developers
Java for android developersAly Abdelkareem
 

Similar to JS-02-JavaScript-Objects.ppt (20)

Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Using class and object java
Using class and object javaUsing class and object java
Using class and object java
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Object in python tells about object oriented programming in python
Object in python tells about object oriented programming in pythonObject in python tells about object oriented programming in python
Object in python tells about object oriented programming in python
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
com-213-unified-modelling-launguage-programming-theory.pdf
com-213-unified-modelling-launguage-programming-theory.pdfcom-213-unified-modelling-launguage-programming-theory.pdf
com-213-unified-modelling-launguage-programming-theory.pdf
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Lecture 2 classes i
Lecture 2 classes iLecture 2 classes i
Lecture 2 classes i
 
Java for android developers
Java for android developersJava for android developers
Java for android developers
 
Oops
OopsOops
Oops
 
Java
JavaJava
Java
 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

JS-02-JavaScript-Objects.ppt

  • 1. Object Oriented Programming in JavaScript Dr. Charles Severance www.wa4e.com http://www.wa4e.com/code/javascript-objects http://www.wa4e.com/code/javascript-objects.zip
  • 2. Definitions • Class - a template - Dog • Method or Message - A defined capability of a class - bark() • Attribute - A defined data item in a class - color • Object or Instance - A particular instance of a class - Lassie Image CC-By 2.0: https://www.flickr.com/photos/dinnerseries/23570475099
  • 3. Terminology: Class Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields, or properties) and the thing’s behaviors (the things it can do, or methods, operations, or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). http://en.wikipedia.org/wiki/Object- oriented_programming
  • 4. Terminology: Class http://en.wikipedia.org/wiki/Object- oriented_programming A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.
  • 5. Terminology: Instance One can have an instance of a class or a particular object. The instance is the actual object created at runtime. In programmer jargon, the Lassie object is an instance of the Dog class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that’s defined in the object’s class. Object and Instance are often used interchangeably. http://en.wikipedia.org/wiki/Object- oriented_programming
  • 6. Terminology: Method An object’s abilities. In language, methods are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie’s methods. She may have other methods as well, for example sit() or eat() or walk() or save_timmy(). Within the program, using a method usually affects only one particular object; all Dogs can bark, but you need only one particular dog to do the barking Method and Message are often used interchangeably. http://en.wikipedia.org/wiki/Object- oriented_programming
  • 7. Objects in JavaScript • The OO pattern in JavaScript is a little different. • The function is indeed a store and reuse pattern. • The function keyword returns a value which is the function itself - it makes a function!
  • 8. First-Class Functions http://en.wikipedia.org/wiki/First-class_function In computer science, a programming language is said to have first-class functions if it treats functions as first- class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.
  • 10. function PartyAnimal() { this.x = 0; this.party = function () { this.x = this.x + 1; console.log("So far "+this.x); } } an = new PartyAnimal(); an.party(); an.party(); an.party(); This is the template for making PartyAnimal objects. Each PartyAnimal object has a bit of data. Each PartyAnimal object has a bit of code. Create a PartyAnimal object Tell the object to run the party() code. js01.ht m
  • 11. an x: party() function PartyAnimal() { this.x = 0; this.party = function () { this.x = this.x + 1; console.log("So far "+this.x); } } an = new PartyAnimal(); an.party(); an.party(); an.party(); js- 01.htm
  • 13. Object Life Cycle • Objects are created, used, and discarded • Constructors are implicit in JavaScript - natural • A constructor in a class is a special block of statements called when an object is created • Destructors are not provided by JavaScript http://en.wikipedia.org/wiki/Constructor_(computer_scie nce)
  • 14. js03.ht m function PartyAnimal() { this.x = 0; console.log("In the 'constructor'"); this.party = function () { this.x = this.x + 1; console.log("So far "+this.x); } } an = new PartyAnimal(); an.party(); an.party(); an.party();
  • 15. Many Instances • We can create lots of objects - the class is the template for the object. • We can store each distinct object in its own variable. • We call this having multiple instances of the same class. • Each instance has its own copy of the instance variables.
  • 16. function PartyAnimal(nam) { this.x = 0; this.name = nam; console.log("Built "+nam); this.party = function () { this.x = this.x + 1; console.log(nam+"="+this.x); } } s = new PartyAnimal("Sally"); s.party(); j = new PartyAnimal("Jim"); j.party(); s.party(); Constructors can have additional parameters. These can be used to set up instance variables for the particular instance of the class (i.e., for the particular object). js04.ht m
  • 17. s x: name: j x: name: function PartyAnimal(nam) { this.x = 0; this.name = nam; console.log("Built "+nam); this.party = function () { this.x = this.x + 1; console.log(nam+"="+this.x); } } s = new PartyAnimal("Sally"); s.party(); j = new PartyAnimal("Jim"); j.party(); s.party(); js04.ht m
  • 18. Definitions • Class - a template - Dog • Method or Message - A defined capability of a class - bark() • Object or Instance - A particular instance of a class - Lassie • Constructor - A method which is called when the instance / object is created
  • 19. Summary • The key for this lecture is to understand how to read OO documentation for JavaScript and how to use objects. • Building brand new complex objects is more advanced. • It is important to remember that JavaScript uses objects as its “Associative Array”.
  • 20. Acknowledgements / Contributions These slides are Copyright 2010- Charles R. Severance (www.dr-chuck.com) as part of www.wa4e.com and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here
  • 21. Additional Source Information • “Snowman Cookie Cutter” by Didriks is licensed under CC BY https://www.flickr.com/photos/dinnerseries/23570475099 • Photo from the television program Lassie. Lassie watches as Jeff (Tommy Rettig) works on his bike is Public Domain https://en.wikipedia.org/wiki/Lassie - /media/File:Lassie_and_Tommy_Rettig_1956.JPG

Editor's Notes

  1. Note from Chuck. Please retain and maintain this page as you remix and republish these materials. Please add any of your own improvements or contributions.