SlideShare a Scribd company logo
Function-and-Prototype Defined
Classes in JavaScript
Hong Langford
June 24, 2017
Overview
• Objects and prototypes
• Student-Grade example
• this keyword
• Constructor function
• How does the new operator work in JavaScript
• Inheritance with the prototype chain
• The instanceof operator in JavaScript
• References
Objects and Prototypes
• Objects are JavaScript’s fundamental data
structure. Intuitively, an object represents a
table relating strings to values. But when you
dig deeper, there is a fair amount of
machinery that goes into objects.
• Like many object-oriented languages,
JavaScript provides support for
implementation inheritance: the reuse of code
or data through a dynamic delegation
mechanism. But unlike many conventional
languages, JavaScript’s inheritance mechanism
is based on prototypes rather than classes. For
many programmers, JavaScript is the
first object-oriented language they encounter
without classes.
• JavaScript classes introduced in ECMAScript
2015 (ES6) are primarily syntactical sugar over
JavaScript's existing prototype-based
inheritance. The class syntax is not introducing
a new object-oriented inheritance model to
JavaScript. JavaScript classes provide a much
simpler and clearer syntax to create objects
and deal with inheritance for programmers
who are used to classes.
• In many languages, every object is an instance
of an associated class, which provides code
shared between all its instances. JavaScript,
by contrast, has no built-in notion of classes.
Instead, objects inherit from other objects.
Every object is associated with some other
object, known as its prototype. Working with
prototypes can be different from classes,
although many concepts from traditional
object-oriented languages still carry over.
Student-Grade Example
function Student(x) {
this.name = x;
this.grade = [];
}
Student.prototype = {
addGrade: function(x) {
this.grade.push(x);
}
};
var Tom = new Student("Tom");
Tom.addGrade(82);
Tom.addGrade(75);
Tom.addGrade(70);
var Sarah = new Student("Sarah");
Sarah.addGrade(83);
Sarah.addGrade(88);
Sarah.addGrade(91);
var Vu = new Student("Vu");
Vu.addGrade(85);
Vu.addGrade(79);
Vu.addGrade(84);
console.log(Tom, Sarah, Vu);
this keyword
• A function's this keyword behaves a little
differently in JavaScript compared to other
languages. In most cases, the value of this is
determined by how a function is called. It may
be different each time the function is called.
Constructor Function
• Constructor function
– A "constructor" in JavaScript is "just" a function
that happens to be called with the new operator.
How does the new operator work in
JavaScript?
• The new operator uses the
internal [[Construct]] method, and it basically
does the following:
– Initializes a new object
– Sets the internal [[Prototype]] of this object, pointing
to the constructor function prototype property.
– If the constructor function's prototype property is
not an object (but a primitive value, such as a
Number, String, Boolean, Undefined or
Null), Object.prototype is used instead.
– After creating the object, it calls the function,
providing the object as its this value.
– If the return value of the called function, is a
primitive, the object created internally is returned.
– Otherwise, if an object is returned, the object
created internally is lost.
• { name: 'Tom', grade: [ 82, 75, 70 ] } { name:
'Sarah', grade: [ 83, 88, 91 ] } { name: 'Vu',
grade: [ 85, 79, 84 ] }
Function-and-prototype defined classes in JavaScript
Inheritance with the Prototype Chain
• When it comes to inheritance, JavaScript only has
one construct: objects. Each object has a private
property (referred to as [[Prototype]] ) which
holds a link to another object called
its prototype. That prototype object has a
prototype of its own, and so on until an object is
reached with null as its prototype. By
definition, null has no prototype, and acts as the
final link in this prototype chain.
• Nearly all objects in JavaScript are instances
of Object constructor which sits on the top of
a prototype chain. And the prototype of
Object’s prototype is not pointing anywhere
but just null .
The instanceof operator in JavaScript
• The instanceof operator tests presence
of constructor.prototype in object's prototype
chain.
References
1. David Herman, Effective JavaScript: 68 Specific
Ways to Harness the Power of JavaScript
(Effective Software Development
Series), Addison-Wesley Professional, 1 edition
(December 6, 2012).
2. https://stackoverflow.com/questions/6750880/
how-does-the-new-operator-work-in-javascript
3. https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference
Thank You!

More Related Content

What's hot (20)

PPTX
Small Lambda Talk @Booster2015
Martin (高馬丁) Skarsaune
 
PPT
4. Classes and Methods
Nilesh Dalvi
 
PDF
Java keywords
Ravi_Kant_Sahu
 
PPSX
Strings in Java
Hitesh-Java
 
PDF
What To Leave Implicit
Martin Odersky
 
PPTX
What To Leave Implicit
Martin Odersky
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PPTX
Java 101 Intro to Java Programming - Exercises
agorolabs
 
PPT
3. Data types and Variables
Nilesh Dalvi
 
PPTX
What is String in Java?
RAKESH P
 
PDF
Preparing for Scala 3
Martin Odersky
 
PPTX
Introduction to JavaScript
Rangana Sampath
 
PDF
String handling(string class)
Ravi Kant Sahu
 
ODP
String Interpolation in Scala
Knoldus Inc.
 
PPT
Core java concepts
Chikugehlot
 
PPTX
2CPP04 - Objects and Classes
Michael Heron
 
PPT
6. Exception Handling
Nilesh Dalvi
 
PPTX
Scala basic
Nguyen Tuan
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPT
7. Multithreading
Nilesh Dalvi
 
Small Lambda Talk @Booster2015
Martin (高馬丁) Skarsaune
 
4. Classes and Methods
Nilesh Dalvi
 
Java keywords
Ravi_Kant_Sahu
 
Strings in Java
Hitesh-Java
 
What To Leave Implicit
Martin Odersky
 
What To Leave Implicit
Martin Odersky
 
Wrapper classes
Ravi_Kant_Sahu
 
Java 101 Intro to Java Programming - Exercises
agorolabs
 
3. Data types and Variables
Nilesh Dalvi
 
What is String in Java?
RAKESH P
 
Preparing for Scala 3
Martin Odersky
 
Introduction to JavaScript
Rangana Sampath
 
String handling(string class)
Ravi Kant Sahu
 
String Interpolation in Scala
Knoldus Inc.
 
Core java concepts
Chikugehlot
 
2CPP04 - Objects and Classes
Michael Heron
 
6. Exception Handling
Nilesh Dalvi
 
Scala basic
Nguyen Tuan
 
9. Input Output in java
Nilesh Dalvi
 
7. Multithreading
Nilesh Dalvi
 

Similar to Function-and-prototype defined classes in JavaScript (20)

PDF
Prototype 120102020133-phpapp02
plutoone TestTwo
 
PPT
Advanced Javascript
relay12
 
PPT
Advanced Javascript
Manikanda kumar
 
PPT
Advanced Javascript
Adieu
 
PPTX
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
PDF
JS Level Up: Prototypes
Vernon Kesner
 
PDF
Prototype
Aditya Gaur
 
PDF
The prototype property
Hernan Mammana
 
PPTX
Javascript Prototype Visualized
军 沈
 
PDF
JavaScript Essentials
Triphon Statkov
 
PPTX
Js: master prototypes
Barak Drechsler
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
PPTX
Ian 20150116 java script oop
LearningTech
 
PDF
How prototype works in java script?
InnovationM
 
PDF
JavaScript Inheritance
Jussi Pohjolainen
 
PPT
JavaScript In Object Oriented Way
Borey Lim
 
PPT
Java script unleashed
Dibyendu Tiwary
 
PPT
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
PPTX
Object oriented javascript
Usman Mehmood
 
PDF
Javascript
Aditya Gaur
 
Prototype 120102020133-phpapp02
plutoone TestTwo
 
Advanced Javascript
relay12
 
Advanced Javascript
Manikanda kumar
 
Advanced Javascript
Adieu
 
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
JS Level Up: Prototypes
Vernon Kesner
 
Prototype
Aditya Gaur
 
The prototype property
Hernan Mammana
 
Javascript Prototype Visualized
军 沈
 
JavaScript Essentials
Triphon Statkov
 
Js: master prototypes
Barak Drechsler
 
Advanced JavaScript
Stoyan Stefanov
 
Ian 20150116 java script oop
LearningTech
 
How prototype works in java script?
InnovationM
 
JavaScript Inheritance
Jussi Pohjolainen
 
JavaScript In Object Oriented Way
Borey Lim
 
Java script unleashed
Dibyendu Tiwary
 
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
Object oriented javascript
Usman Mehmood
 
Javascript
Aditya Gaur
 
Ad

Recently uploaded (20)

PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Ad

Function-and-prototype defined classes in JavaScript

  • 1. Function-and-Prototype Defined Classes in JavaScript Hong Langford June 24, 2017
  • 2. Overview • Objects and prototypes • Student-Grade example • this keyword • Constructor function • How does the new operator work in JavaScript • Inheritance with the prototype chain • The instanceof operator in JavaScript • References
  • 3. Objects and Prototypes • Objects are JavaScript’s fundamental data structure. Intuitively, an object represents a table relating strings to values. But when you dig deeper, there is a fair amount of machinery that goes into objects.
  • 4. • Like many object-oriented languages, JavaScript provides support for implementation inheritance: the reuse of code or data through a dynamic delegation mechanism. But unlike many conventional languages, JavaScript’s inheritance mechanism is based on prototypes rather than classes. For many programmers, JavaScript is the first object-oriented language they encounter without classes.
  • 5. • JavaScript classes introduced in ECMAScript 2015 (ES6) are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance for programmers who are used to classes.
  • 6. • In many languages, every object is an instance of an associated class, which provides code shared between all its instances. JavaScript, by contrast, has no built-in notion of classes. Instead, objects inherit from other objects. Every object is associated with some other object, known as its prototype. Working with prototypes can be different from classes, although many concepts from traditional object-oriented languages still carry over.
  • 7. Student-Grade Example function Student(x) { this.name = x; this.grade = []; } Student.prototype = { addGrade: function(x) { this.grade.push(x); } }; var Tom = new Student("Tom"); Tom.addGrade(82); Tom.addGrade(75); Tom.addGrade(70);
  • 8. var Sarah = new Student("Sarah"); Sarah.addGrade(83); Sarah.addGrade(88); Sarah.addGrade(91); var Vu = new Student("Vu"); Vu.addGrade(85); Vu.addGrade(79); Vu.addGrade(84); console.log(Tom, Sarah, Vu);
  • 9. this keyword • A function's this keyword behaves a little differently in JavaScript compared to other languages. In most cases, the value of this is determined by how a function is called. It may be different each time the function is called.
  • 10. Constructor Function • Constructor function – A "constructor" in JavaScript is "just" a function that happens to be called with the new operator.
  • 11. How does the new operator work in JavaScript? • The new operator uses the internal [[Construct]] method, and it basically does the following: – Initializes a new object – Sets the internal [[Prototype]] of this object, pointing to the constructor function prototype property. – If the constructor function's prototype property is not an object (but a primitive value, such as a Number, String, Boolean, Undefined or Null), Object.prototype is used instead.
  • 12. – After creating the object, it calls the function, providing the object as its this value. – If the return value of the called function, is a primitive, the object created internally is returned. – Otherwise, if an object is returned, the object created internally is lost.
  • 13. • { name: 'Tom', grade: [ 82, 75, 70 ] } { name: 'Sarah', grade: [ 83, 88, 91 ] } { name: 'Vu', grade: [ 85, 79, 84 ] }
  • 15. Inheritance with the Prototype Chain • When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property (referred to as [[Prototype]] ) which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.
  • 16. • Nearly all objects in JavaScript are instances of Object constructor which sits on the top of a prototype chain. And the prototype of Object’s prototype is not pointing anywhere but just null .
  • 17. The instanceof operator in JavaScript • The instanceof operator tests presence of constructor.prototype in object's prototype chain.
  • 18. References 1. David Herman, Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript (Effective Software Development Series), Addison-Wesley Professional, 1 edition (December 6, 2012). 2. https://stackoverflow.com/questions/6750880/ how-does-the-new-operator-work-in-javascript 3. https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference

Editor's Notes

  • #8: C:\Users\Hong\WebstormProjects\studentgradeproj Question: what’s the result?
  • #9: Question: what’s the result?
  • #10: Later, we will see what the value of this is in our example.
  • #13: So in our example, the value of this is Tom when executing var Tom = new Student("Tom"); Any questions? See Example 2 returnobject.js // defining constructors function C() { this.name = "Jack"; //return {a:1}; }; function D() { this.name = "Linda"; } var o = new C(); var o2 = new D(); console.log(o, o2); //C { name: 'Jack' } D { name: 'Linda' }
  • #15: Question: Why use prototypes? Using prototypes can save memory. Any questions?
  • #16: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain Mention example.
  • #18: See Example 3 instanceof.js in webstorm. // defining constructors function C() {} function D() {} var o = new C(); // true, because: Object.getPrototypeOf(o) === C.prototype console.log(o instanceof C); // false, because D.prototype is nowhere in o's prototype chain console.log(o instanceof D); console.log(o instanceof Object); // true, because: console.log(C.prototype instanceof Object) // true