SlideShare una empresa de Scribd logo
1 de 20
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.
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)
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?

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Arrays in Java
Arrays in Java Arrays in Java
Arrays in Java
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
C++
C++C++
C++
 

Similar a Js: master prototypes

Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
Chamnap Chhorn
 
javascript
javascript javascript
javascript
Kaya Ota
 

Similar a Js: master prototypes (20)

Javascript closures
Javascript closures Javascript closures
Javascript closures
 
Object oriented programming in JavaScript
Object oriented programming in JavaScriptObject oriented programming in JavaScript
Object oriented programming in JavaScript
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
DeclaringConstructir.pptx
DeclaringConstructir.pptxDeclaringConstructir.pptx
DeclaringConstructir.pptx
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
Function-and-prototype defined classes in JavaScript
Function-and-prototype defined classes in JavaScriptFunction-and-prototype defined classes in JavaScript
Function-and-prototype defined classes in JavaScript
 
How prototype works in java script?
How prototype works in java script?How prototype works in java script?
How prototype works in java script?
 
Java Constructor
Java ConstructorJava Constructor
Java Constructor
 
Prototype
PrototypePrototype
Prototype
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
javascript
javascript javascript
javascript
 

Más de Barak Drechsler

Más de Barak Drechsler (8)

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

Último

一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
F
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
ydyuyu
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Monica Sydney
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Monica Sydney
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
JOHNBEBONYAP1
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
pxcywzqs
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
ayvbos
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 

Último (20)

Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency Dallas
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girls
 

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.
  • 8.
  • 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)
  • 16.
  • 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

Notas del editor

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