SlideShare a Scribd company logo
INHERITANCE	
  
About	
  Inheritance	
  
•  Code	
  reuse	
  is	
  important	
  
– Inheritance	
  can	
  help	
  
•  JavaScript	
  does	
  not	
  have	
  classes,	
  so	
  no	
  special	
  
keyword	
  for	
  extending	
  
•  In	
  JS	
  you	
  have	
  lot	
  of	
  ways	
  to	
  do	
  inheritance	
  
– Classical	
  Pa@erns	
  
– Modern	
  Pa@erns	
  or	
  Prototypal	
  Inheritance	
  
Understanding	
  JS	
  Inheritance	
  
•  JS	
  is	
  not	
  class	
  based,	
  it’s	
  prototype-­‐based!	
  
•  Object	
  inherit	
  from	
  another	
  object	
  
•  JS	
  contains	
  syntax	
  and	
  features	
  that	
  make	
  it	
  
seem	
  class	
  based	
  
	
  
Understanding	
  Prototypes	
  
•  Prototype	
  is	
  an	
  object	
  from	
  which	
  other	
  
objects	
  inherit	
  properGes	
  
•  Any	
  object	
  can	
  be	
  a	
  prototype	
  
•  Every	
  object	
  has	
  internal	
  __proto__	
  property	
  
Example	
  
var parent = {
method1: function() { print("A"); }
}
var child = {
__proto__: parent,
method2: function() { print("B"); }
}
// If method1 is not found in child, look it from
// prototype!
child.method1(); // A
child.method2(); // B
__proto__	
  
•  __proto__	
  is	
  depricated	
  and	
  should	
  not	
  be	
  
used	
  (but	
  it	
  works)	
  
•  To	
  get	
  the	
  prototype,	
  use	
  
– Object.getPrototypeOf(object)	
  
•  It’s	
  read	
  only!	
  
•  How	
  to	
  set?	
  
– Proposal:	
  Object.setPrototypeOf(obj,	
  prototype)	
  
•  Not	
  possible	
  to	
  change	
  the	
  __proto__	
  ..!	
  
FuncGon	
  Object	
  
•  When	
  wriGng	
  
– function Animal() { }
•  Lot	
  of	
  things	
  happens!	
  
– Two	
  objects	
  created:	
  	
  
•  1)	
  Animal	
  
•  2)	
  Animal.prototype	
  
– Animal.prototype	
  has	
  a	
  property	
  constructor,	
  that	
  
points	
  to	
  Animal	
  
FuncGon	
  Object	
  
// This is just a function. Dog is Function object!
function Dog (name) {
this.name = (name);
}
var spot = new Dog("Spot");
// true
print(spot instanceof Object);
// true
print(Dog instanceof Function);
// true
print(Dog instanceof Object);
FuncGon	
  Object	
  
function sum1(a, b) {
return a + b;
}
// =>
var sum2 = new Function("a","b", "return a+b;");
print(sum1(2,2)); // 4
print(sum2(2,2)); // 4
print(sum2.length); // number of args = 2
print(sum2.toString());
The	
  “new”	
  Operator	
  
function Person() {
this.name = “Jack”;
}
// Normal function call
Person();
// Object creation
var p = new Person();
JavaScript Inheritance
Example	
  
function Cat() { }
// c.__proto__ points to Cat.prototype!
var c = new Cat();
// true
print(c.__proto__ === Cat.prototype);
// c inherites Cat.prototype!
Cat.prototype.age = 12;
// 12!
print(c.age);
Example	
  
function Cat() { this.name = "Jack"; }
var c = new Cat();
// true
print(c.__proto__ === Cat.prototype);
// c inherites Cat.prototype! Let's add stuff.
Cat.prototype.age = 12;
Cat.prototype.saySomething = function() {
print(this.name + ": hello!");
}
// 12!
print(c.age);
// "Jack: hello!"
c.saySomething();
/** PERSON **/
function Person() { }
var jack = new Person();
// jack inherites Person.prototype!
print(jack.__proto__ === Person.prototype);
Person.prototype.age = 18;
print(jack.age); // 18;
//** STUDENT **/
function Student() { }
// Let's now change the prototype of Student.
// Now Student.prototype points to Person.
var temp = new Person();
Student.prototype = temp;
var tina = new Student();
// tina inherites Student.prototype.. which is now temp!
print(tina.__proto__ === Student.prototype); // true
print(tina.__proto__ === temp); // true
// Now tina inherites Student.prototype, which is
// Person object, which inherites the Person.prototype..
print(tina.age); // 18!
Example	
  
/** Person **/
function Person() {
this.name = "Jack";
}
// Adding functionality to the prototype..
Person.prototype.say = function() {
print(this.name + ”: hello!");
}
/** Student **/
function Student() { }
// Inheritance
Student.prototype = new Person();
/** Test **/
var student = new Student();
student.say(); // Jack: Hello
Person.prototype	
  
say()	
  
new	
  Person()	
  
name	
  =	
  “Jack”	
  
__proto__	
  
Example	
  
/** Person **/
function Person() {
this.name = "Jack";
}
// Adding functionality to the prototype.. What is this??
Person.prototype.say = function() {
print(this.name + ”: hello!");
}
/** Student **/
function Student() { }
// Inheritance
Student.prototype = new Person();
/** Test **/
var student = new Student();
student.say(); // Jack: Hello
Person.prototype	
  
say()	
  
new	
  Person()	
  
name	
  =	
  “Jack”	
  
__proto__	
  
new	
  Student()	
  
__proto__	
  
FuncGon	
  Object	
  
•  Every	
  funcGon	
  in	
  JS	
  is	
  a	
  FuncGon	
  object	
  
•  When	
  	
  
– var spot = new Dog(“spot”);
•  Then	
  spot’s	
  __proto__	
  points	
  to	
  
Dog.prototype!	
  
•  If	
  a	
  property	
  cannot	
  be	
  found	
  in	
  an	
  object,	
  it	
  
is	
  searched	
  for	
  in	
  that	
  object's	
  prototype.	
  
Example	
  
// here's the constructor:
function Point() { }
var a = new Point();
print (a.x); // undefined
// set up the prototype object to have some values:
Point.prototype = { x: 10, y: 20 };
// or you could do this:
Point.prototype.z = 30;
// make a new Point object
// (a object gets an implicit reference to Point.prototype object)
var a = new Point();
// Since a does not hold a property, let's look it from Point.prototype
print (a.x);
Example	
  
// here's the constructor:
function Point() {
this.x = 10;
this.y = 20;
}
// set up the prototype object to have some values:
Point.prototype.z = 40;
// make a new Point object
// (a object gets an implicit reference to Point.prototype object)
var a = new Point();
// Since a does not hold a property, let's look it from Point.prototype
print (a.z);
//** POINT **
function Point() {
}
// set up the prototype object to have some values:
Point.prototype = { x: 10, y: 20 };
/** PIXEL **/
function Pixel() {
}
Pixel.prototype = new Point();
Pixel.prototype.color = "red";
// make a new Point object
// (a object gets an implicit reference to Point.prototype object)
var a = new Pixel();
var b = new Pixel();
a.color = "blue";
// Since a does not hold a property, let's look it from Point.prototype
print (a.color);
print (b.color);
About	
  constructors	
  
•  Prototype	
  properGes	
  of	
  FuncGons	
  have	
  a	
  
constructor	
  property:	
  
– var	
  dog	
  =	
  new	
  Dog();	
  
– dog.constructor	
  ==	
  Dog;	
  //	
  TRUE	
  
•  This	
  will	
  break	
  when	
  doing	
  inheritance!	
  
/** Person **/
function Person() {
}
Person.prototype.name = "Jack";
/** Student **/
function Student() {
this.id = "12345";
}
// Inheritance
Student.prototype = new Person();
Student.prototype.id = "12345";
/** Test **/
var student = new Student();
student.age = 22;
print(student.age)
print(student.name);
print(student.id);
var person = new Person();
print(person.constructor === Person); // TRUE
var student = new Student();
print(student.constructor === Student); // FALSE
/** Person **/
function Person() {
}
Person.prototype.name = "Jack";
/** Student **/
function Student() {
this.id = "12345";
}
// Inheritance
Student.prototype = new Person();
Student.prototype.id = "12345";
// FIX
Student.prototype.constructor = Student;
/** Test **/
var student = new Student();
student.age = 22;
print(student.age)
print(student.name);
print(student.id);
var person = new Person();
print(person.constructor === Person); // TRUE
var student = new Student();
print(student.constructor === Student); // FALSE
Inheritance:	
  Prototypal	
  
•  In	
  EcmaScript	
  5	
  a	
  protypal	
  inheritance	
  pa@ern	
  is	
  
part	
  of	
  the	
  language	
  
–  Object.create()	
  
–  var	
  child	
  =	
  Object.create(parent);	
  
•  The	
  create-­‐funcGon	
  
function create(o) {
function F() {}
f.prototype = o;
return new F();
}
Example	
  
function Point(x,y) {
this.x = x;
this.y = y;
}
var pixel = Object.create(new Point(12,0));
pixel.color = "red";
print(pixel.x);
print(pixel.color);

More Related Content

PPSX
Javascript variables and datatypes
PPTX
Spring boot Introduction
PDF
Basics of JavaScript
PPTX
What Is Express JS?
PDF
The New JavaScript: ES6
PPTX
Java script
PDF
ES2015 / ES6: Basics of modern Javascript
PPTX
Constructor in java
Javascript variables and datatypes
Spring boot Introduction
Basics of JavaScript
What Is Express JS?
The New JavaScript: ES6
Java script
ES2015 / ES6: Basics of modern Javascript
Constructor in java

What's hot (20)

PPT
JavaScript Tutorial
PPTX
Angular Data Binding
PDF
ECMA Script
PDF
JavaScript - Chapter 11 - Events
PPTX
Core java complete ppt(note)
PDF
JavaScript - Chapter 7 - Advanced Functions
PDF
JavaScript - Chapter 10 - Strings and Arrays
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
PDF
JavaScript - Chapter 8 - Objects
PDF
ES6 presentation
PPTX
Form using html and java script validation
PPTX
JSON: The Basics
PPTX
Javascript functions
PPT
JDBC – Java Database Connectivity
PPTX
Introduction to Spring Framework
PDF
Intro to Asynchronous Javascript
PPT
C# Exceptions Handling
PPT
Introduction to Javascript
PDF
JavaScript Programming
PDF
Angular Dependency Injection
JavaScript Tutorial
Angular Data Binding
ECMA Script
JavaScript - Chapter 11 - Events
Core java complete ppt(note)
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 8 - Objects
ES6 presentation
Form using html and java script validation
JSON: The Basics
Javascript functions
JDBC – Java Database Connectivity
Introduction to Spring Framework
Intro to Asynchronous Javascript
C# Exceptions Handling
Introduction to Javascript
JavaScript Programming
Angular Dependency Injection
Ad

Viewers also liked (10)

PDF
New ES6 Hotness
PDF
JavaScript Inheritence
KEY
2012 oct-12 - java script inheritance
PDF
Javascript foundations: Inheritance
ODP
Basic inheritance in JavaScript
PPT
Advanced JavaScript
PDF
JavaScript: The prototype Property
PDF
libGDX: Screens, Fonts and Preferences
PDF
JavaScript Prototype and Module Pattern
PPTX
Prototype & Inheritance in JavaScript
New ES6 Hotness
JavaScript Inheritence
2012 oct-12 - java script inheritance
Javascript foundations: Inheritance
Basic inheritance in JavaScript
Advanced JavaScript
JavaScript: The prototype Property
libGDX: Screens, Fonts and Preferences
JavaScript Prototype and Module Pattern
Prototype & Inheritance in JavaScript
Ad

Similar to JavaScript Inheritance (20)

PPTX
Ian 20150116 java script oop
PPT
Advanced Javascript
PPT
Advanced Javascript
PPT
Advanced Javascript
PDF
Prototype
PDF
Prototype 120102020133-phpapp02
PPTX
Javascript Prototypal Inheritance - Big Picture
PDF
JS Level Up: Prototypes
PPT
Java script unleashed
PDF
JavaScript Essentials
PDF
The prototype property
PPT
Beginning Object-Oriented JavaScript
PPT
JavaScript In Object Oriented Way
PPTX
Oojs 1.1
PPTX
Object oriented javascript
KEY
Javascript tid-bits
PPTX
Javascript Prototype Visualized
PPTX
Object Oriented JavaScript
PPT
Javascript Object Oriented Programming
PPTX
Ajaxworld
Ian 20150116 java script oop
Advanced Javascript
Advanced Javascript
Advanced Javascript
Prototype
Prototype 120102020133-phpapp02
Javascript Prototypal Inheritance - Big Picture
JS Level Up: Prototypes
Java script unleashed
JavaScript Essentials
The prototype property
Beginning Object-Oriented JavaScript
JavaScript In Object Oriented Way
Oojs 1.1
Object oriented javascript
Javascript tid-bits
Javascript Prototype Visualized
Object Oriented JavaScript
Javascript Object Oriented Programming
Ajaxworld

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
Box2D and libGDX
PDF
libGDX: Tiled Maps
PDF
libGDX: User Input and Frame by Frame Animation
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
libGDX: User Input
PDF
Implementing a Simple Game using libGDX
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Creating Games for Asha - platform
PDF
Intro to Asha UI
PDF
Intro to Java ME and Asha Platform
Moved to Speakerdeck
Java Web Services
Box2D and libGDX
libGDX: Tiled Maps
libGDX: User Input and Frame by Frame Animation
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
libGDX: User Input
Implementing a Simple Game using libGDX
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Games for Asha - platform
Intro to Asha UI
Intro to Java ME and Asha Platform

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Modernizing your data center with Dell and AMD
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
Modernizing your data center with Dell and AMD
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Monthly Chronicles - July 2025
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

JavaScript Inheritance

  • 2. About  Inheritance   •  Code  reuse  is  important   – Inheritance  can  help   •  JavaScript  does  not  have  classes,  so  no  special   keyword  for  extending   •  In  JS  you  have  lot  of  ways  to  do  inheritance   – Classical  Pa@erns   – Modern  Pa@erns  or  Prototypal  Inheritance  
  • 3. Understanding  JS  Inheritance   •  JS  is  not  class  based,  it’s  prototype-­‐based!   •  Object  inherit  from  another  object   •  JS  contains  syntax  and  features  that  make  it   seem  class  based    
  • 4. Understanding  Prototypes   •  Prototype  is  an  object  from  which  other   objects  inherit  properGes   •  Any  object  can  be  a  prototype   •  Every  object  has  internal  __proto__  property  
  • 5. Example   var parent = { method1: function() { print("A"); } } var child = { __proto__: parent, method2: function() { print("B"); } } // If method1 is not found in child, look it from // prototype! child.method1(); // A child.method2(); // B
  • 6. __proto__   •  __proto__  is  depricated  and  should  not  be   used  (but  it  works)   •  To  get  the  prototype,  use   – Object.getPrototypeOf(object)   •  It’s  read  only!   •  How  to  set?   – Proposal:  Object.setPrototypeOf(obj,  prototype)   •  Not  possible  to  change  the  __proto__  ..!  
  • 7. FuncGon  Object   •  When  wriGng   – function Animal() { } •  Lot  of  things  happens!   – Two  objects  created:     •  1)  Animal   •  2)  Animal.prototype   – Animal.prototype  has  a  property  constructor,  that   points  to  Animal  
  • 8. FuncGon  Object   // This is just a function. Dog is Function object! function Dog (name) { this.name = (name); } var spot = new Dog("Spot"); // true print(spot instanceof Object); // true print(Dog instanceof Function); // true print(Dog instanceof Object);
  • 9. FuncGon  Object   function sum1(a, b) { return a + b; } // => var sum2 = new Function("a","b", "return a+b;"); print(sum1(2,2)); // 4 print(sum2(2,2)); // 4 print(sum2.length); // number of args = 2 print(sum2.toString());
  • 10. The  “new”  Operator   function Person() { this.name = “Jack”; } // Normal function call Person(); // Object creation var p = new Person();
  • 12. Example   function Cat() { } // c.__proto__ points to Cat.prototype! var c = new Cat(); // true print(c.__proto__ === Cat.prototype); // c inherites Cat.prototype! Cat.prototype.age = 12; // 12! print(c.age);
  • 13. Example   function Cat() { this.name = "Jack"; } var c = new Cat(); // true print(c.__proto__ === Cat.prototype); // c inherites Cat.prototype! Let's add stuff. Cat.prototype.age = 12; Cat.prototype.saySomething = function() { print(this.name + ": hello!"); } // 12! print(c.age); // "Jack: hello!" c.saySomething();
  • 14. /** PERSON **/ function Person() { } var jack = new Person(); // jack inherites Person.prototype! print(jack.__proto__ === Person.prototype); Person.prototype.age = 18; print(jack.age); // 18; //** STUDENT **/ function Student() { } // Let's now change the prototype of Student. // Now Student.prototype points to Person. var temp = new Person(); Student.prototype = temp; var tina = new Student(); // tina inherites Student.prototype.. which is now temp! print(tina.__proto__ === Student.prototype); // true print(tina.__proto__ === temp); // true // Now tina inherites Student.prototype, which is // Person object, which inherites the Person.prototype.. print(tina.age); // 18!
  • 15. Example   /** Person **/ function Person() { this.name = "Jack"; } // Adding functionality to the prototype.. Person.prototype.say = function() { print(this.name + ”: hello!"); } /** Student **/ function Student() { } // Inheritance Student.prototype = new Person(); /** Test **/ var student = new Student(); student.say(); // Jack: Hello Person.prototype   say()   new  Person()   name  =  “Jack”   __proto__  
  • 16. Example   /** Person **/ function Person() { this.name = "Jack"; } // Adding functionality to the prototype.. What is this?? Person.prototype.say = function() { print(this.name + ”: hello!"); } /** Student **/ function Student() { } // Inheritance Student.prototype = new Person(); /** Test **/ var student = new Student(); student.say(); // Jack: Hello Person.prototype   say()   new  Person()   name  =  “Jack”   __proto__   new  Student()   __proto__  
  • 17. FuncGon  Object   •  Every  funcGon  in  JS  is  a  FuncGon  object   •  When     – var spot = new Dog(“spot”); •  Then  spot’s  __proto__  points  to   Dog.prototype!   •  If  a  property  cannot  be  found  in  an  object,  it   is  searched  for  in  that  object's  prototype.  
  • 18. Example   // here's the constructor: function Point() { } var a = new Point(); print (a.x); // undefined // set up the prototype object to have some values: Point.prototype = { x: 10, y: 20 }; // or you could do this: Point.prototype.z = 30; // make a new Point object // (a object gets an implicit reference to Point.prototype object) var a = new Point(); // Since a does not hold a property, let's look it from Point.prototype print (a.x);
  • 19. Example   // here's the constructor: function Point() { this.x = 10; this.y = 20; } // set up the prototype object to have some values: Point.prototype.z = 40; // make a new Point object // (a object gets an implicit reference to Point.prototype object) var a = new Point(); // Since a does not hold a property, let's look it from Point.prototype print (a.z);
  • 20. //** POINT ** function Point() { } // set up the prototype object to have some values: Point.prototype = { x: 10, y: 20 }; /** PIXEL **/ function Pixel() { } Pixel.prototype = new Point(); Pixel.prototype.color = "red"; // make a new Point object // (a object gets an implicit reference to Point.prototype object) var a = new Pixel(); var b = new Pixel(); a.color = "blue"; // Since a does not hold a property, let's look it from Point.prototype print (a.color); print (b.color);
  • 21. About  constructors   •  Prototype  properGes  of  FuncGons  have  a   constructor  property:   – var  dog  =  new  Dog();   – dog.constructor  ==  Dog;  //  TRUE   •  This  will  break  when  doing  inheritance!  
  • 22. /** Person **/ function Person() { } Person.prototype.name = "Jack"; /** Student **/ function Student() { this.id = "12345"; } // Inheritance Student.prototype = new Person(); Student.prototype.id = "12345"; /** Test **/ var student = new Student(); student.age = 22; print(student.age) print(student.name); print(student.id); var person = new Person(); print(person.constructor === Person); // TRUE var student = new Student(); print(student.constructor === Student); // FALSE
  • 23. /** Person **/ function Person() { } Person.prototype.name = "Jack"; /** Student **/ function Student() { this.id = "12345"; } // Inheritance Student.prototype = new Person(); Student.prototype.id = "12345"; // FIX Student.prototype.constructor = Student; /** Test **/ var student = new Student(); student.age = 22; print(student.age) print(student.name); print(student.id); var person = new Person(); print(person.constructor === Person); // TRUE var student = new Student(); print(student.constructor === Student); // FALSE
  • 24. Inheritance:  Prototypal   •  In  EcmaScript  5  a  protypal  inheritance  pa@ern  is   part  of  the  language   –  Object.create()   –  var  child  =  Object.create(parent);   •  The  create-­‐funcGon   function create(o) { function F() {} f.prototype = o; return new F(); }
  • 25. Example   function Point(x,y) { this.x = x; this.y = y; } var pixel = Object.create(new Point(12,0)); pixel.color = "red"; print(pixel.x); print(pixel.color);