SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
JAVASCRIPT PROTOTYPE
VISUALIZED
come and bite me if you still don’t understand the concept of prototype after
this presentation
PROTOTYPE OF FUNCTION
Every function is born with a prototype object, it’s used as the shared
prototype(parent) of the objects created by this function( invoked as constructor
function).
The prototype is initially an empty object, you can add members to it. Such all its
“children” have access to these members( properties, methods ) as well.
F
prototyp
e
constructo
r
F.prototype
F.prototype.constructor
PROTOTYPE OF OBJECT
Every Object is born referencing to a prototype object(parent) by a secret property
__proto__.
__proto__
WHAT’S THE RELATIONSHIP?
F
prototyp
e
constructo
r
__proto__
What’s the relationship between these two
concept?
?
var Person = function(name, age){
this.name = name;
this.age = age;
};
Person.prototype.sayMyName = function(){
console.log("I'm " + this.name);
};
var linus = new Person('Linus Torvalds');
linus.sayMyName();
CONSTRUCT A NEW OBJECT
Person
prototyp
e
constructo
r
__proto__
linus.sayMyName
sayMyNamename: Linus
Torvaldsage: 46
name: Alice
age: 17
name: Bob
age: 18
new
let’s walk through the following simple
code to understand the whole process
there’re 3 steps are done by the
javascript engine whenever a new
instance is created, let’s see them in
action:1. create a bare object
2. create a link “__proto__” points to the
prototype of the constructor function
3. execute the function body of the
constructor function
PROTOTYPE CHAIN
Object
prototyp
e
constructo
r
F
prototyp
e
constructo
r
__proto____proto__
ne
w
built-
in
This is the prototype
chain
__proto__
Object.prototype is the top of prototype
chain, Object.prototype doesn’t have
prototype
OBJECT LITERAL
Object
prototyp
e
constructo
r
__proto__
ne
w
anObjec
t
var anObject = {};
// is equal to
var anObject = new Object();
built-
in
RESOLVE PROPERTY
var value = anObject.someProperty;
Object
prototyp
e
constructo
r
F1
prototyp
e
constructo
r
__proto____proto__
built-
in
F2
prototyp
e
constructo
r
__proto__
anObject
Lookup “someProperty” on
anObject
1
2
b
not found, continue
lookup “someProperty” on
its __proto__ object
found, return
value
2a
found, return
value
3a
3
b
not found, continue
lookup “someProperty” on
its __proto__ object
found, return
value
4a
found, return
value
5a
4
b
not found, continue
lookup “someProperty” on
its __proto__ object
5
b
not found, return
undefined
let’s see how javascript engine resolve
property lookup
MODIFY PROPERTY
anObject.someProperty = “some property”;
Object
prototyp
e
constructo
r
F1
prototyp
e
constructo
r
__proto____proto__
built-
in
F2
prototyp
e
constructo
r
__proto__
somePropert
y
somePropert
y
anObject
When you do changes to a property of
an object, it always affect the current
object only. If the property doesn’t
exist, property is added to the object. It
won’t look up to the prototype chain.
PSEUDO-CLASSICAL INHERITANCE
Develope
r
prototyp
e
constructo
r
__proto__
Person
prototyp
e
constructo
r
F
prototyp
e
new
prototyp
e
constructo
r
prototyp
e
constructo
r
I put the prototype and function up
down just to leave room for following
animation, don’t be confused here, it’s
the same stuff
let’s walk through the following code to see how
classical inheritance is achieved
let’s remove the irrelevant part now
Now we’ve achieved pseudo-classical
inheritance !!!
WRONG PSEUDO-CLASSICAL
INHERITANCE
Develope
r
prototyp
e
constructo
r
__proto__
Person
prototyp
e
constructo
r
new
Develope
r
prototyp
e
constructo
r
__proto__
Person
prototyp
e
constructo
r
F
prototyp
e
new
wrong
correc
t
let’s look at a frequent mistake of doing classical
inheritance
Why is the first example is wrong ? Well, I
wouldn’t say it’s always wrong, but in
most cases, it’s wrong. Because the
subclass Developer’s prototype is an
instance of Person Class, that means it’s a
special individual person. And in most
case, the Person Constructor would
require some arguments to initialize a
Person instance, such as: name, age … Do
we want these properties on the prototype
of Developer ? No ! What we want is a bare
object which just has a “__proto__” points
to the prototype of Person Class. That’s
exactly how the second example does.
Through a temporary constructor function
F which does nothing in its constructor, it
will create a bare object points to the
prototype of F which is equal to prototype
OBJECT.CREATE
In ES5, a method is included to implement inheritance:
// utility function for inheritance
var inherit = function(proto){
function F(){}
F.prototype = proto;
return new F();
};
our simple version
Object.create
ES5 version, more
powerful
PROTOTYPAL INHERITANCE
Develope
r
Person
__proto__
let’s walk through the following code to see how
prototypal inheritance is achieved
linus
__proto__
That’s it, we accomplished prototypal inheritance. You
can see how much easier prototypal inheritance is than
Classical inheritance. That’s because it completely
discard Constructor parts. And more importantly, in
javascript, the essence of inheritance is through the
“__proto__” link between objects, aka. prototype chain.
At the heart of the classical inheritance, it’s also using
the prototype chain achieving inheritance. Only it add
an extra layer of “Constructor” to simulate the “Class”
concept from other language: java, c#... to make it more
comfortable for developers from those languages. Even
it feels somehow comfortable at first, but without truly
understanding of the essence of prototypal inheritance,
You’ll get lost and confused at last !!! So that’s why a lot
of javascript gurus advocate prototypal inheritance and
recommend avoid using of classical inheritance.
__PROTO__ OF FUNCTION
Every function is a “child” of Function.prototype
F
prototyp
e
constructo
r
__proto__
Function
prototyp
e
constructo
r
function
Empty(){}
Object
prototyp
e
constructo
r
__proto__
__proto__
__proto__
ne
w
__proto__
built-
in
built-
in
JAVASCRIPT OBJECT LAYOUT
This is a graph of object layout I grab it
from some online website. If you
understand the prototype concept
correctly, you can easily understand the
graph. But personally, I feel it’s a little
messy with all the arrows floating around.
Anyway, it’s a great graph for your
reference when you forget the
relationship.
THE END
Thank you for watching!
Author: Jun Shen
I’m an enthusiast about javascript
Javascript Rocks!!!
linked in: https://www.linkedin.com/in/jun-shen-55b01959

More Related Content

PPTX
Js: master prototypes
Barak Drechsler
 
PPTX
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
PPT
Javascript
guest03a6e6
 
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
 
PDF
The New JavaScript: ES6
Rob Eisenberg
 
PPTX
Javascript functions
Alaref Abushaala
 
PPTX
9. ES6 | Let And Const | TypeScript | JavaScript
pcnmtutorials
 
PDF
JavaScript Programming
Sehwan Noh
 
Js: master prototypes
Barak Drechsler
 
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
Javascript
guest03a6e6
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
 
The New JavaScript: ES6
Rob Eisenberg
 
Javascript functions
Alaref Abushaala
 
9. ES6 | Let And Const | TypeScript | JavaScript
pcnmtutorials
 
JavaScript Programming
Sehwan Noh
 

What's hot (20)

PDF
ES6 presentation
ritika1
 
PPTX
Javascript 101
Shlomi Komemi
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PDF
Asynchronous JavaScript Programming
Haim Michael
 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
 
PPT
Angular 8
Sunil OS
 
PPT
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
PPT
Javascript
mussawir20
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PPTX
An Intro into webpack
Squash Apps Pvt Ltd
 
PPTX
Angular Directives
Malla Reddy University
 
PPTX
Java script
Abhishek Kesharwani
 
PPT
Document Object Model
chomas kandar
 
PDF
Javascript
Momentum Design Lab
 
ES6 presentation
ritika1
 
Javascript 101
Shlomi Komemi
 
jQuery for beginners
Arulmurugan Rajaraman
 
Asynchronous JavaScript Programming
Haim Michael
 
JavaScript: Variables and Functions
Jussi Pohjolainen
 
Angular 8
Sunil OS
 
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
Javascript
mussawir20
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Introduction to Javascript
Amit Tyagi
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
An Intro into webpack
Squash Apps Pvt Ltd
 
Angular Directives
Malla Reddy University
 
Java script
Abhishek Kesharwani
 
Document Object Model
chomas kandar
 
Ad

Viewers also liked (20)

PDF
Prototype
Aditya Gaur
 
PDF
JavaScript Prototype and Module Pattern
Narendra Sisodiya
 
PDF
Object Oriented Programming in JavaScript
zand3rs
 
PDF
Prototype-based Programming with JavaScript
lienhard
 
PPTX
Object Oriented Programming In JavaScript
Forziatech
 
PPTX
Prototype & Inheritance in JavaScript
Sunny Sharma
 
PDF
Oo java script class construction
Ken Collins
 
PDF
03. Introduccion a JavaScript y JQuery
Danae Aguilar Guzmán
 
ODP
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
PDF
CSS Sanity with Sass: The Inverted Triangle Approach
Julie Kuehl
 
PPTX
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
PDF
JavaScript: The prototype Property
Guillermo Paz
 
PPTX
SASS - CSS with Superpower
Kanchha kaji Prajapati
 
PPTX
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
raja kvk
 
KEY
Advanced sass/compass
Nick Cooley
 
PDF
Smart CSS theming with Sass and Compass
Mihaela
 
PPTX
Syntactically awesome stylesheets (Sass)
Tahmina Khatoon
 
PDF
JavaScript OOPs
Johnson Chou
 
PPTX
Advanced JavaScript Concepts
Naresh Kumar
 
PDF
Sass - Getting Started with Sass!
Eric Sembrat
 
Prototype
Aditya Gaur
 
JavaScript Prototype and Module Pattern
Narendra Sisodiya
 
Object Oriented Programming in JavaScript
zand3rs
 
Prototype-based Programming with JavaScript
lienhard
 
Object Oriented Programming In JavaScript
Forziatech
 
Prototype & Inheritance in JavaScript
Sunny Sharma
 
Oo java script class construction
Ken Collins
 
03. Introduccion a JavaScript y JQuery
Danae Aguilar Guzmán
 
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
CSS Sanity with Sass: The Inverted Triangle Approach
Julie Kuehl
 
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
JavaScript: The prototype Property
Guillermo Paz
 
SASS - CSS with Superpower
Kanchha kaji Prajapati
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
raja kvk
 
Advanced sass/compass
Nick Cooley
 
Smart CSS theming with Sass and Compass
Mihaela
 
Syntactically awesome stylesheets (Sass)
Tahmina Khatoon
 
JavaScript OOPs
Johnson Chou
 
Advanced JavaScript Concepts
Naresh Kumar
 
Sass - Getting Started with Sass!
Eric Sembrat
 
Ad

Similar to Javascript Prototype Visualized (20)

PDF
Inverting Dependencies
Luc Trudeau
 
PDF
How prototype works in java script?
InnovationM
 
PPTX
Javascript Objects Deep Dive
Manish Jangir
 
PPT
Java script unleashed
Dibyendu Tiwary
 
PDF
JavaScript Essentials
Triphon Statkov
 
PPT
Advanced JavaScript
Fu Cheng
 
PDF
Monad Fact #6
Philip Schwarz
 
PDF
Prototype 120102020133-phpapp02
plutoone TestTwo
 
PDF
The prototype property
Hernan Mammana
 
PPTX
All of Javascript
Togakangaroo
 
PPTX
Javascript talk
Suresh Jayanty
 
PPS
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla
 
PPT
Jump Start To Ooad And Design Patterns
Lalit Kale
 
PDF
JavaScript, qué hermoso eres
Alea Soluciones, S.L.
 
PPTX
All of javascript
Togakangaroo
 
PPT
Javascript Object Oriented Programming
Bunlong Van
 
PPTX
Object oriented javascript
Usman Mehmood
 
PPTX
My Presentation ITPdcjsdicjscisuchc.pptx
lonewolfvivek99
 
Inverting Dependencies
Luc Trudeau
 
How prototype works in java script?
InnovationM
 
Javascript Objects Deep Dive
Manish Jangir
 
Java script unleashed
Dibyendu Tiwary
 
JavaScript Essentials
Triphon Statkov
 
Advanced JavaScript
Fu Cheng
 
Monad Fact #6
Philip Schwarz
 
Prototype 120102020133-phpapp02
plutoone TestTwo
 
The prototype property
Hernan Mammana
 
All of Javascript
Togakangaroo
 
Javascript talk
Suresh Jayanty
 
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla
 
Jump Start To Ooad And Design Patterns
Lalit Kale
 
JavaScript, qué hermoso eres
Alea Soluciones, S.L.
 
All of javascript
Togakangaroo
 
Javascript Object Oriented Programming
Bunlong Van
 
Object oriented javascript
Usman Mehmood
 
My Presentation ITPdcjsdicjscisuchc.pptx
lonewolfvivek99
 

Recently uploaded (20)

PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Software Development Company | KodekX
KodekX
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Software Development Company | KodekX
KodekX
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Doc9.....................................
SofiaCollazos
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 

Javascript Prototype Visualized

  • 1. JAVASCRIPT PROTOTYPE VISUALIZED come and bite me if you still don’t understand the concept of prototype after this presentation
  • 2. PROTOTYPE OF FUNCTION Every function is born with a prototype object, it’s used as the shared prototype(parent) of the objects created by this function( invoked as constructor function). The prototype is initially an empty object, you can add members to it. Such all its “children” have access to these members( properties, methods ) as well. F prototyp e constructo r F.prototype F.prototype.constructor
  • 3. PROTOTYPE OF OBJECT Every Object is born referencing to a prototype object(parent) by a secret property __proto__. __proto__
  • 4. WHAT’S THE RELATIONSHIP? F prototyp e constructo r __proto__ What’s the relationship between these two concept? ?
  • 5. var Person = function(name, age){ this.name = name; this.age = age; }; Person.prototype.sayMyName = function(){ console.log("I'm " + this.name); }; var linus = new Person('Linus Torvalds'); linus.sayMyName(); CONSTRUCT A NEW OBJECT Person prototyp e constructo r __proto__ linus.sayMyName sayMyNamename: Linus Torvaldsage: 46 name: Alice age: 17 name: Bob age: 18 new let’s walk through the following simple code to understand the whole process there’re 3 steps are done by the javascript engine whenever a new instance is created, let’s see them in action:1. create a bare object 2. create a link “__proto__” points to the prototype of the constructor function 3. execute the function body of the constructor function
  • 6. PROTOTYPE CHAIN Object prototyp e constructo r F prototyp e constructo r __proto____proto__ ne w built- in This is the prototype chain __proto__ Object.prototype is the top of prototype chain, Object.prototype doesn’t have prototype
  • 7. OBJECT LITERAL Object prototyp e constructo r __proto__ ne w anObjec t var anObject = {}; // is equal to var anObject = new Object(); built- in
  • 8. RESOLVE PROPERTY var value = anObject.someProperty; Object prototyp e constructo r F1 prototyp e constructo r __proto____proto__ built- in F2 prototyp e constructo r __proto__ anObject Lookup “someProperty” on anObject 1 2 b not found, continue lookup “someProperty” on its __proto__ object found, return value 2a found, return value 3a 3 b not found, continue lookup “someProperty” on its __proto__ object found, return value 4a found, return value 5a 4 b not found, continue lookup “someProperty” on its __proto__ object 5 b not found, return undefined let’s see how javascript engine resolve property lookup
  • 9. MODIFY PROPERTY anObject.someProperty = “some property”; Object prototyp e constructo r F1 prototyp e constructo r __proto____proto__ built- in F2 prototyp e constructo r __proto__ somePropert y somePropert y anObject When you do changes to a property of an object, it always affect the current object only. If the property doesn’t exist, property is added to the object. It won’t look up to the prototype chain.
  • 10. PSEUDO-CLASSICAL INHERITANCE Develope r prototyp e constructo r __proto__ Person prototyp e constructo r F prototyp e new prototyp e constructo r prototyp e constructo r I put the prototype and function up down just to leave room for following animation, don’t be confused here, it’s the same stuff let’s walk through the following code to see how classical inheritance is achieved let’s remove the irrelevant part now Now we’ve achieved pseudo-classical inheritance !!!
  • 11. WRONG PSEUDO-CLASSICAL INHERITANCE Develope r prototyp e constructo r __proto__ Person prototyp e constructo r new Develope r prototyp e constructo r __proto__ Person prototyp e constructo r F prototyp e new wrong correc t let’s look at a frequent mistake of doing classical inheritance Why is the first example is wrong ? Well, I wouldn’t say it’s always wrong, but in most cases, it’s wrong. Because the subclass Developer’s prototype is an instance of Person Class, that means it’s a special individual person. And in most case, the Person Constructor would require some arguments to initialize a Person instance, such as: name, age … Do we want these properties on the prototype of Developer ? No ! What we want is a bare object which just has a “__proto__” points to the prototype of Person Class. That’s exactly how the second example does. Through a temporary constructor function F which does nothing in its constructor, it will create a bare object points to the prototype of F which is equal to prototype
  • 12. OBJECT.CREATE In ES5, a method is included to implement inheritance: // utility function for inheritance var inherit = function(proto){ function F(){} F.prototype = proto; return new F(); }; our simple version Object.create ES5 version, more powerful
  • 13. PROTOTYPAL INHERITANCE Develope r Person __proto__ let’s walk through the following code to see how prototypal inheritance is achieved linus __proto__ That’s it, we accomplished prototypal inheritance. You can see how much easier prototypal inheritance is than Classical inheritance. That’s because it completely discard Constructor parts. And more importantly, in javascript, the essence of inheritance is through the “__proto__” link between objects, aka. prototype chain. At the heart of the classical inheritance, it’s also using the prototype chain achieving inheritance. Only it add an extra layer of “Constructor” to simulate the “Class” concept from other language: java, c#... to make it more comfortable for developers from those languages. Even it feels somehow comfortable at first, but without truly understanding of the essence of prototypal inheritance, You’ll get lost and confused at last !!! So that’s why a lot of javascript gurus advocate prototypal inheritance and recommend avoid using of classical inheritance.
  • 14. __PROTO__ OF FUNCTION Every function is a “child” of Function.prototype F prototyp e constructo r __proto__ Function prototyp e constructo r function Empty(){} Object prototyp e constructo r __proto__ __proto__ __proto__ ne w __proto__ built- in built- in
  • 15. JAVASCRIPT OBJECT LAYOUT This is a graph of object layout I grab it from some online website. If you understand the prototype concept correctly, you can easily understand the graph. But personally, I feel it’s a little messy with all the arrows floating around. Anyway, it’s a great graph for your reference when you forget the relationship.
  • 16. THE END Thank you for watching! Author: Jun Shen I’m an enthusiast about javascript Javascript Rocks!!! linked in: https://www.linkedin.com/in/jun-shen-55b01959

Editor's Notes

  • #3: You can think of the prototype object is equal to super class at some degree
  • #11: var Person = function(name, age){ this.name = name; this.age = age; }; Person.prototype.selfIntroduce = function(){ return "I'm " + this.name + ", " + this.age + " years old."; }; // utility function for inheritance var inherit = function(proto){ function F(){} F.prototype = proto; return new F(); }; var Developer = function(name, age, language){ Person.call(this, name, age); this.language = language; }; // Developer's prototype to inherite from Person's prototype Developer.prototype = inherit(Person.prototype); Developer.prototype.constructor = Developer; var jun = new Developer('sj', 30, 'javascript'); jun.selfIntroduce();
  • #12: Don't create new Animal to inherit it There is a well-known, but wrong way of inhereting, when instead of Rabbit.prototype = inherit(Animal.prototype) people use: // inherit from Animal Rabbit.prototype = new Animal() As a result, we get a new Animal object in prototype. Inheritance works here, becausenew Animal naturally inherits Animal.prototype. … But who said that new Animal() can be called like without the name? The constructor may strictly require arguments and die without them. Actually, the problem is more conceptual than that. We don’t want to create an Animal. We just want to inherit from it. That’s why Rabbit.prototype = inherit(Animal.prototype) is preferred. The neat inheritance without side-effects.
  • #14: var person = { selfIntroduce: function(){ return "I'm " + this.name + ", " + this.age + " years old."; } }; // prototypal inherite from person var developer = Object.create(person); // create an instance of developer var jun = Object.create(developer); jun.name = 'jun'; jun.age = 30; jun.selfIntroduce();