SlideShare a Scribd company logo
Prototypes
By Barak Drechsler
First things first…
objects it all about
objects
JS Objects
• JavaScript is designed on a simple object-based paradigm.
• An object is a collection of properties
• A property is an association between a name (or key) and a value.
• Objects can be linked to other objects, via prototypes
Creating Objects in JS
var x = new Object(); // with new operator
var x = {}; // object literal
var x = Object.create({}); // with object.create
JavaScript is prototype-
based with first-class
functions, making it a multi-
paradigm language,
supporting object-oriented,
imperative, and functional
programming styles.
Classes Inheritance
A class is like a blueprint — a description of the object to be created.
Classes inherit from classes and create subclass relationships: hierarchical class
taxonomies.
Prototypical Inheritance
Prototypal Inheritance: A prototype is a working object instance. Objects inherit
directly from other objects.
Js: master prototypes
Prototypical Chain
The prototypical chain, is a chain that links between objects
When a JS is looking for an object property it looks up recursively through the
prototype chain up to Object.prototype, and if it can’t find the property it will return
undefined.
How can we see an object
prototype?
The prototype, which is marked in official documents [[Prototype]], not to be confused with
function.prototype.
Can be accessed via:
Object.getPrototypeOf(obj)
Or via nonstandard way that was self defined by the browsers (not in the ECMA spec)
__proto__ property
Prototypical Chain - basic
How To Define a prototype?
var x = new ConstructorFunc(); // constructor function and new operator.
var x = Object.create({}); // with object.create, given param is the prototype
var x = {};
// both operations below, should be avoided, they are very costly at
performance
x.__proto__ = {}; // not standard
The relation between
constructors and
prototypes...
Two very confusing terms in JS language
[[Prototype]] or __proto__ - the actual linkage to the prototype, and prototype
chain.
prototype - property of any function, defines the prototype of a new object that will
be initiated by this function with the new operator.
The New Operator
function _new() {
// Create a new object that inherits from the
// constructor's prototype.
var that = Object.create(this.prototype);
// Invoke the constructor, binding this- to
// the new object.
var other = this.apply(that, arguments);
// If its return value isn't an object,
// substitute the new object.
return (typeof other === 'object' && other) || that;
});
Basic Inheritance JS
ES5 Prototypes Inheritance
ES6 Prototypes Inheritance (syntactic sugar of classes )
JavaScript classes introduced in ECMAScript 6 are 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. (MDN)
Js: master prototypes
Be aware of instanceof
function Agent(){}
var agentHijaker = {};
Agent.prototype = agentHijaker;
var fakeAgent = Object.create(agentHijaker);
fakeAgent instanceof Agent // true - wtf?
Problems with classical
inheritance
- Fragile
- Tight coupling
- Hard to change later
- Creates unneeded hierarchies
Other ways...
- Factory functions (rather than constructor functions)
- Concatenative inheritance via object compositions
- Functional Inheritance
Questions?

More Related Content

PDF
JavaScript guide 2020 Learn JavaScript
PPTX
React workshop
PPT
Introduction to JavaScript
PPTX
PDF
Bootstrap
PPTX
Css Complete Notes
PDF
Javascript Arrow function
PPTX
Introduction to Node js
JavaScript guide 2020 Learn JavaScript
React workshop
Introduction to JavaScript
Bootstrap
Css Complete Notes
Javascript Arrow function
Introduction to Node js

What's hot (20)

PPTX
Javascript Prototype Visualized
PDF
Closures in Javascript
PPTX
Core java complete ppt(note)
PDF
Advanced javascript
PDF
JavaScript Inheritance
PDF
JavaScript Programming
PDF
PPT
Advanced Javascript
PPTX
C# Constructors
ODP
Java 9/10/11 - What's new and why you should upgrade
PPTX
Inner classes in java
PDF
Java 8 Lambda Expressions & Streams
PDF
Java 8 Lambda Expressions
PPTX
Javascript this keyword
PPTX
Constructor in java
PDF
Object Oriented Programming in JavaScript
PPT
JDBC – Java Database Connectivity
PPT
Core java concepts
PPTX
Javascript Design Patterns
PPTX
Friend function & friend class
Javascript Prototype Visualized
Closures in Javascript
Core java complete ppt(note)
Advanced javascript
JavaScript Inheritance
JavaScript Programming
Advanced Javascript
C# Constructors
Java 9/10/11 - What's new and why you should upgrade
Inner classes in java
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions
Javascript this keyword
Constructor in java
Object Oriented Programming in JavaScript
JDBC – Java Database Connectivity
Core java concepts
Javascript Design Patterns
Friend function & friend class
Ad

Similar to Js: master prototypes (20)

PDF
Javascript closures
 
PPTX
Object oriented programming in JavaScript
PPTX
JavaScript OOPS Implimentation
PPT
Advanced JavaScript
PPTX
Object oriented javascript
PPTX
JavaScript Beyond jQuery
PPTX
DeclaringConstructir.pptx
PPTX
Javascript Objects Deep Dive
PPT
Advanced Javascript
PPT
Advanced Javascript
PPTX
Ajaxworld
PDF
JavaScript - Chapter 8 - Objects
PPTX
JavaScript in Object-Oriented Way
PPTX
Function-and-prototype defined classes in JavaScript
PDF
How prototype works in java script?
PPTX
Java PPT OOPS prepared by Abhinav J.pptx
PPTX
Java Constructor
PDF
Prototype
PPTX
Ch-2ppt.pptx
PPTX
Javascript Prototypal Inheritance - Big Picture
Javascript closures
 
Object oriented programming in JavaScript
JavaScript OOPS Implimentation
Advanced JavaScript
Object oriented javascript
JavaScript Beyond jQuery
DeclaringConstructir.pptx
Javascript Objects Deep Dive
Advanced Javascript
Advanced Javascript
Ajaxworld
JavaScript - Chapter 8 - Objects
JavaScript in Object-Oriented Way
Function-and-prototype defined classes in JavaScript
How prototype works in java script?
Java PPT OOPS prepared by Abhinav J.pptx
Java Constructor
Prototype
Ch-2ppt.pptx
Javascript Prototypal Inheritance - Big Picture
Ad

More from Barak Drechsler (8)

PPTX
Stay lazy, use lerna
PPTX
Atomic javascript
PPTX
Room service (worker) please!
PPTX
From MVC to Component Based Architecture
PPTX
Fundamentals of Browser Rendering Css Overview PT 2
PPTX
Fundamentals of Browser Rendering Css Overview PT 1
PPTX
Understanding the flex layout
PPTX
Javascript: master this
Stay lazy, use lerna
Atomic javascript
Room service (worker) please!
From MVC to Component Based Architecture
Fundamentals of Browser Rendering Css Overview PT 2
Fundamentals of Browser Rendering Css Overview PT 1
Understanding the flex layout
Javascript: master this

Recently uploaded (20)

PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
Funds Management Learning Material for Beg
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
artificial intelligence overview of it and more
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
innovation process that make everything different.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
WebRTC in SignalWire - troubleshooting media negotiation
An introduction to the IFRS (ISSB) Stndards.pdf
presentation_pfe-universite-molay-seltan.pptx
Funds Management Learning Material for Beg
Module 1 - Cyber Law and Ethics 101.pptx
artificial intelligence overview of it and more
international classification of diseases ICD-10 review PPT.pptx
innovation process that make everything different.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx
PptxGenJS_Demo_Chart_20250317130215833.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
The Internet -By the Numbers, Sri Lanka Edition
SASE Traffic Flow - ZTNA Connector-1.pdf
Introuction about WHO-FIC in ICD-10.pptx
Unit-1 introduction to cyber security discuss about how to secure a system
introduction about ICD -10 & ICD-11 ppt.pptx
QR Codes Qr codecodecodecodecocodedecodecode
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...

Js: master prototypes

  • 2. First things first… objects it all about objects
  • 3. JS Objects • JavaScript is designed on a simple object-based paradigm. • An object is a collection of properties • A property is an association between a name (or key) and a value. • Objects can be linked to other objects, via prototypes
  • 4. Creating Objects in JS var x = new Object(); // with new operator var x = {}; // object literal var x = Object.create({}); // with object.create
  • 5. JavaScript is prototype- based with first-class functions, making it a multi- paradigm language, supporting object-oriented, imperative, and functional programming styles.
  • 6. Classes Inheritance A class is like a blueprint — a description of the object to be created. Classes inherit from classes and create subclass relationships: hierarchical class taxonomies.
  • 7. Prototypical Inheritance Prototypal Inheritance: A prototype is a working object instance. Objects inherit directly from other objects.
  • 9. Prototypical Chain The prototypical chain, is a chain that links between objects When a JS is looking for an object property it looks up recursively through the prototype chain up to Object.prototype, and if it can’t find the property it will return undefined.
  • 10. How can we see an object prototype? The prototype, which is marked in official documents [[Prototype]], not to be confused with function.prototype. Can be accessed via: Object.getPrototypeOf(obj) Or via nonstandard way that was self defined by the browsers (not in the ECMA spec) __proto__ property
  • 12. How To Define a prototype? var x = new ConstructorFunc(); // constructor function and new operator. var x = Object.create({}); // with object.create, given param is the prototype var x = {}; // both operations below, should be avoided, they are very costly at performance x.__proto__ = {}; // not standard
  • 13. The relation between constructors and prototypes... Two very confusing terms in JS language [[Prototype]] or __proto__ - the actual linkage to the prototype, and prototype chain. prototype - property of any function, defines the prototype of a new object that will be initiated by this function with the new operator.
  • 14. The New Operator function _new() { // Create a new object that inherits from the // constructor's prototype. var that = Object.create(this.prototype); // Invoke the constructor, binding this- to // the new object. var other = this.apply(that, arguments); // If its return value isn't an object, // substitute the new object. return (typeof other === 'object' && other) || that; });
  • 15. Basic Inheritance JS ES5 Prototypes Inheritance ES6 Prototypes Inheritance (syntactic sugar of classes ) JavaScript classes introduced in ECMAScript 6 are 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. (MDN)
  • 17. Be aware of instanceof function Agent(){} var agentHijaker = {}; Agent.prototype = agentHijaker; var fakeAgent = Object.create(agentHijaker); fakeAgent instanceof Agent // true - wtf?
  • 18. Problems with classical inheritance - Fragile - Tight coupling - Hard to change later - Creates unneeded hierarchies
  • 19. Other ways... - Factory functions (rather than constructor functions) - Concatenative inheritance via object compositions - Functional Inheritance

Editor's Notes

  • #9: http://yycjs.com/the-weird-parts/#slide19
  • #20: Inspierd by https://medium.com/javascript-scene/3-different-kinds-of-prototypal-inheritance-es6-edition-32d777fa16c9#.y7p0e930i