SlideShare a Scribd company logo
ObjectOriented Programmingin
JavaScript
Dr. Charles Severance
www.wa4e.com
http://www.wa4e.com/code/javascript-objects
http://www.wa4e.com/code/javascript-objects.zip
Definitions
• Class - atemplate- Dog
• MethodorMessage-A definedcapabilityofa class - bark()
• Attribute- Adefineddataitem in aclass -color
• Object orInstance- Aparticularinstance ofa class - Lassie
Image CC-By 2.0: https://www.flickr.com/photos/dinnerseries/23570475099
Terminology:Class
Defines theabstractcharacteristicsof athing(object), includingthething'scharacteristics (its
attributes,fields,orproperties)and thething’sbehaviors (the thingsit can do,ormethods,
operations,orfeatures).One might saythataclass is ablueprint orfactorythatdescribes the
natureof something.Forexample,the class Dog wouldconsist of traitssharedbyalldogs,such
as breedandfurcolor(characteristics), andthe abilitytobarkandsit (behaviors).
http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology:Class
http://en.wikipedia.org/wiki/Object-oriented_programming
Apattern(exemplar) of aclass. The class of Dogdefines all
possible dogsbylistingthe characteristics andbehaviors
theycan have;the object Lassieis one particulardog,with
particularversions ofthe characteristics. ADoghas fur;
Lassiehasbrown-and-whitefur.
Terminology:Instance
Onecan have aninstance ofa class oraparticularobject.The instanceis theactualobject
created atruntime.In programmerjargon,the Lassieobject is aninstanceof theDogclass.The
set of values ofthe attributesof aparticularobject is calledits state.The object consists of state
andthebehaviorthat’sdefinedin the object’s class.
Object andInstance areoften used interchangeably.
http://en.wikipedia.org/wiki/Object-oriented_programming
Terminology:Method
Anobject’s abilities. In language,methods areverbs. Lassie,beingaDog,has theabilityto bark.
Sobark() is oneofLassie’s methods. Shemayhave other methods as well,forexample sit() or
eat() orwalk()orsave_timmy(). Within theprogram,usinga methodusuallyaffectsonly one
particularobject;allDogs canbark,butyouneed onlyoneparticulardogto dothebarking
Method andMessage areoften used interchangeably.
http://en.wikipedia.org/wiki/Object-oriented_programming
Objects in JavaScript
• The OO patterninJavaScriptis alittledifferent.
• The functionis indeed astoreandreuse pattern.
• Thefunctionkeywordreturnsavaluewhich is thefunctionitself - itmakes a
function!
First-ClassFunctions
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 SampleClass
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 thetemplate formaking
PartyAnimalobjects.
Each PartyAnimalobject has a
bit ofdata.
Each PartyAnimalobject has abit of
code.
CreateaPartyAnimalobject
Tell the object torunthe party()
code. js01.htm
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
ObjectLife Cycle
http://en.wikipedia.org/wiki/Constructor_(computer_science)
Object Life Cycle
• Objects arecreated,used, anddiscarded
• Constructorsareimplicit in JavaScript- natural
• Aconstructorina class is a special blockof statementscalled whenan object
is created
• Destructors arenotprovidedby JavaScript
http://en.wikipedia.org/wiki/Constructor_(computer_science)
js03.htm
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
• Wecan createlots ofobjects -the class is thetemplate forthe object.
• Wecan storeeach distinct object inits own variable.
• Wecall this havingmultiple instances of thesame class.
• Each instancehas its owncopy ofthe 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();
Constructorscanhaveadditional
parameters. Thesecanbeusedtoset up
instancevariablesforthe particular
instanceofthe class(i.e., forthe
particularobject).
js04.htm
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 - atemplate- Dog
• MethodorMessage-A definedcapabilityofa class - bark()
• Object orInstance- Aparticularinstance ofa class - Lassie
• Constructor- Amethod which is called when theinstance /object is created
Summary
• The key forthis lecture is tounderstandhowtoreadOO documentationfor
JavaScriptandhowtouse objects.
• Buildingbrandnewcomplex objects is more advanced.
• Itis importanttoremember thatJavaScriptuses 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
ContinuenewContributorsandTranslatorshere
Additional Source Information
• “SnowmanCookieCutter”byDidriksislicensedunderCCBY
https://www.flickr.com/photos/dinnerseries/23570475099
• PhotofromthetelevisionprogramLassie.Lassiewatchesas Jeff(TommyRettig)worksonhisbikeisPublicDomain
https://en.wikipedia.org/wiki/Lassie- /media/File:Lassie_and_Tommy_Rettig_1956.JPG

More Related Content

What's hot (18)

PDF
Scala: A brief tutorial
Oliver Szymanski
 
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
PPTX
Scala Refactoring for Fun and Profit
Tomer Gabel
 
KEY
RubyistのためのObjective-C入門
S Akai
 
PDF
jQuery-1-Ajax
guestcf600a
 
PPTX
Anonymous Classes: Behind the Mask
Mark Baker
 
PDF
Hello Swift Final 5/5 - Structures and Classes
Cody Yun
 
PPTX
Mootools class
ARIF MAHMUD RANA
 
PDF
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
NETFest
 
PPTX
Iniciando com jquery
Danilo Sousa
 
PDF
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
PDF
Few simple-type-tricks in scala
Ruslan Shevchenko
 
PDF
Lecture 04
Nguyen Thanh Xuan
 
PDF
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
PPTX
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Tomer Gabel
 
PDF
JS OO and Closures
Jussi Pohjolainen
 
KEY
Week3
Will Gaybrick
 
PDF
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
Donny Wals
 
Scala: A brief tutorial
Oliver Szymanski
 
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Scala Refactoring for Fun and Profit
Tomer Gabel
 
RubyistのためのObjective-C入門
S Akai
 
jQuery-1-Ajax
guestcf600a
 
Anonymous Classes: Behind the Mask
Mark Baker
 
Hello Swift Final 5/5 - Structures and Classes
Cody Yun
 
Mootools class
ARIF MAHMUD RANA
 
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
NETFest
 
Iniciando com jquery
Danilo Sousa
 
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Few simple-type-tricks in scala
Ruslan Shevchenko
 
Lecture 04
Nguyen Thanh Xuan
 
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Tomer Gabel
 
JS OO and Closures
Jussi Pohjolainen
 
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
Donny Wals
 

Similar to Best Guide for Javascript Objects (20)

PPT
JS-02-JavaScript-Objects.ppt
MadhukarReddy74
 
PPTX
Ian 20150116 java script oop
LearningTech
 
PPTX
Object oriented javascript
Usman Mehmood
 
PPTX
Web Host_G4.pptx
RNithish1
 
PPTX
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
PPTX
Computer programming 2 Lesson 3
MLG College of Learning, Inc
 
PPTX
8. objects & classes
M H Buddhika Ariyaratne
 
PPT
14 Defining classes
maznabili
 
PPTX
Lecture 4
talha ijaz
 
PPTX
JavaScript OOPS Implimentation
Usman Mehmood
 
PDF
Object-oriented Programming-with C#
Doncho Minkov
 
PDF
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
KaviShetty
 
PPTX
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
PPTX
Object oriented programming
msneha
 
PPTX
Slide - FPT University - PFP191 - Chapter 11 - Objects
cMai28
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Object Oriented Javascript part2
Usman Mehmood
 
PPT
WEB DESIGNING VNSGU UNIT 4 JAVASCRIPT OBJECTS
divyapatel123440
 
PPTX
Javascript Objects and Functions
Gitanjali wagh
 
PPTX
Javascript Objects and Functions
Manoj \/ishwakarma
 
JS-02-JavaScript-Objects.ppt
MadhukarReddy74
 
Ian 20150116 java script oop
LearningTech
 
Object oriented javascript
Usman Mehmood
 
Web Host_G4.pptx
RNithish1
 
Understanding-Objects-in-Javascript.pptx
MariaTrinidadTumanga
 
Computer programming 2 Lesson 3
MLG College of Learning, Inc
 
8. objects & classes
M H Buddhika Ariyaratne
 
14 Defining classes
maznabili
 
Lecture 4
talha ijaz
 
JavaScript OOPS Implimentation
Usman Mehmood
 
Object-oriented Programming-with C#
Doncho Minkov
 
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
KaviShetty
 
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
Object oriented programming
msneha
 
Slide - FPT University - PFP191 - Chapter 11 - Objects
cMai28
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Object Oriented Javascript part2
Usman Mehmood
 
WEB DESIGNING VNSGU UNIT 4 JAVASCRIPT OBJECTS
divyapatel123440
 
Javascript Objects and Functions
Gitanjali wagh
 
Javascript Objects and Functions
Manoj \/ishwakarma
 
Ad

Recently uploaded (20)

PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Ad

Best Guide for Javascript Objects

  • 1. ObjectOriented Programmingin 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 - atemplate- Dog • MethodorMessage-A definedcapabilityofa class - bark() • Attribute- Adefineddataitem in aclass -color • Object orInstance- Aparticularinstance ofa class - Lassie Image CC-By 2.0: https://www.flickr.com/photos/dinnerseries/23570475099
  • 3. Terminology:Class Defines theabstractcharacteristicsof athing(object), includingthething'scharacteristics (its attributes,fields,orproperties)and thething’sbehaviors (the thingsit can do,ormethods, operations,orfeatures).One might saythataclass is ablueprint orfactorythatdescribes the natureof something.Forexample,the class Dog wouldconsist of traitssharedbyalldogs,such as breedandfurcolor(characteristics), andthe abilitytobarkandsit (behaviors). http://en.wikipedia.org/wiki/Object-oriented_programming
  • 4. Terminology:Class http://en.wikipedia.org/wiki/Object-oriented_programming Apattern(exemplar) of aclass. The class of Dogdefines all possible dogsbylistingthe characteristics andbehaviors theycan have;the object Lassieis one particulardog,with particularversions ofthe characteristics. ADoghas fur; Lassiehasbrown-and-whitefur.
  • 5. Terminology:Instance Onecan have aninstance ofa class oraparticularobject.The instanceis theactualobject created atruntime.In programmerjargon,the Lassieobject is aninstanceof theDogclass.The set of values ofthe attributesof aparticularobject is calledits state.The object consists of state andthebehaviorthat’sdefinedin the object’s class. Object andInstance areoften used interchangeably. http://en.wikipedia.org/wiki/Object-oriented_programming
  • 6. Terminology:Method Anobject’s abilities. In language,methods areverbs. Lassie,beingaDog,has theabilityto bark. Sobark() is oneofLassie’s methods. Shemayhave other methods as well,forexample sit() or eat() orwalk()orsave_timmy(). Within theprogram,usinga methodusuallyaffectsonly one particularobject;allDogs canbark,butyouneed onlyoneparticulardogto dothebarking Method andMessage areoften used interchangeably. http://en.wikipedia.org/wiki/Object-oriented_programming
  • 7. Objects in JavaScript • The OO patterninJavaScriptis alittledifferent. • The functionis indeed astoreandreuse pattern. • Thefunctionkeywordreturnsavaluewhich is thefunctionitself - itmakes a function!
  • 8. First-ClassFunctions 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 thetemplate formaking PartyAnimalobjects. Each PartyAnimalobject has a bit ofdata. Each PartyAnimalobject has abit of code. CreateaPartyAnimalobject Tell the object torunthe party() code. js01.htm
  • 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 arecreated,used, anddiscarded • Constructorsareimplicit in JavaScript- natural • Aconstructorina class is a special blockof statementscalled whenan object is created • Destructors arenotprovidedby JavaScript http://en.wikipedia.org/wiki/Constructor_(computer_science)
  • 14. js03.htm 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 • Wecan createlots ofobjects -the class is thetemplate forthe object. • Wecan storeeach distinct object inits own variable. • Wecall this havingmultiple instances of thesame class. • Each instancehas its owncopy ofthe 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(); Constructorscanhaveadditional parameters. Thesecanbeusedtoset up instancevariablesforthe particular instanceofthe class(i.e., forthe particularobject). js04.htm
  • 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 - atemplate- Dog • MethodorMessage-A definedcapabilityofa class - bark() • Object orInstance- Aparticularinstance ofa class - Lassie • Constructor- Amethod which is called when theinstance /object is created
  • 19. Summary • The key forthis lecture is tounderstandhowtoreadOO documentationfor JavaScriptandhowtouse objects. • Buildingbrandnewcomplex objects is more advanced. • Itis importanttoremember thatJavaScriptuses 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 ContinuenewContributorsandTranslatorshere
  • 21. Additional Source Information • “SnowmanCookieCutter”byDidriksislicensedunderCCBY https://www.flickr.com/photos/dinnerseries/23570475099 • PhotofromthetelevisionprogramLassie.Lassiewatchesas Jeff(TommyRettig)worksonhisbikeisPublicDomain https://en.wikipedia.org/wiki/Lassie- /media/File:Lassie_and_Tommy_Rettig_1956.JPG

Editor's Notes

  • #21: 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.