SlideShare una empresa de Scribd logo
1 de 15
inheritance
Learning Javascript foundations


          John Hunter
       12 November 2008
are these the
  same thing?
1         2
 radio                   toggleButton




we have one radio with two buttons
toggleButton


                 toggleUp ()
                 toggleDown ()



toggleButtons share the same behaviour (methods)
we could say they are a particular class of objects
Class
Defines the abstract characteristics of a thing (object),
including the thing's characteristics (its attributes, fields or
properties) and the thing's behaviours (the things it can do, or
methods, operations or features).
One might say that a class is a blueprint or factory that
describes the nature of something.
For example, the class Dog would consist of traits shared by
all dogs, such as breed and fur colour (characteristics), and the
ability to bark and sit (behaviours).
                                               source: wikipedia/Object_orientated_programming
Javascript does not have formal
             classes
it can approximate class based
         programming
but it has other mechanisms for
           inheritance
class-based
  inheritance
       vs.




prototype-based
  inheritance
                nothing to do with the Prototype framework
Classical inheritance
  ToggleButton
                       instantiate

                                     tuningButton
                                         volumeButton



function ToggleButton () { … }
var tuningButton = new ToggleButton();
var volumeButton = new ToggleButton();
Prototypal inheritance
  toggleButton
                          prototype


                                      tuningButton
                                          volumeButton
                  beget




var toggleButton = { … };
var tuningButton = Object.beget(toggleButton);
var volumeButton = Object.beget(toggleButton);
toggleButton

toggleUp ()
toggleDown ()
                             prototype

            Then look here

                                          tuningButton




                                First look here
tuningButton.toggleUp();
class-based approach:
 you always have to create a class blueprint
 in order to create an object

prototype-based approach:
 you can use an existing object as a
 prototype for more objects
Object.beget = function (obj) {
   var F = function () {};
   F.prototype = obj;
   return new F();
};

// myThing = Object.beget(thing);


the beget method returns an object that uses the
           argument as its prototype
                                             source: Doug Crockford
There is more to inheritance
  ...but that’s all for now!
Review
classes of objects share characteristics and behaviour
Javascript supports several inheritance mechanisms
 Class-based (but not true classes)
 Prototype-based
Thanks

Más contenido relacionado

Destacado (10)

2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
 
New ES6 Hotness
New ES6 HotnessNew ES6 Hotness
New ES6 Hotness
 
JavaScript Inheritence
JavaScript  InheritenceJavaScript  Inheritence
JavaScript Inheritence
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
Basic inheritance in JavaScript
Basic inheritance in JavaScriptBasic inheritance in JavaScript
Basic inheritance in JavaScript
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
JavaScript: The prototype Property
JavaScript: The prototype PropertyJavaScript: The prototype Property
JavaScript: The prototype Property
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
JavaScript Prototype and Module Pattern
JavaScript Prototype and Module PatternJavaScript Prototype and Module Pattern
JavaScript Prototype and Module Pattern
 
Prototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScriptPrototype & Inheritance in JavaScript
Prototype & Inheritance in JavaScript
 

Similar a Javascript foundations: Inheritance

Introduction to javascript and yoolkui
Introduction to javascript and yoolkuiIntroduction to javascript and yoolkui
Introduction to javascript and yoolkui
Khou Suylong
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
plutoone TestTwo
 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
 

Similar a Javascript foundations: Inheritance (20)

Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented Programming
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
Introduction to javascript and yoolkui
Introduction to javascript and yoolkuiIntroduction to javascript and yoolkui
Introduction to javascript and yoolkui
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Java script unleashed
Java script unleashedJava script unleashed
Java script unleashed
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Ios development
Ios developmentIos development
Ios development
 
Javascript
JavascriptJavascript
Javascript
 
S12.s01 - Material TP.pdf
S12.s01 - Material TP.pdfS12.s01 - Material TP.pdf
S12.s01 - Material TP.pdf
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
About Python
About PythonAbout Python
About Python
 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Master in javascript
Master in javascriptMaster in javascript
Master in javascript
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 

Javascript foundations: Inheritance

  • 1. inheritance Learning Javascript foundations John Hunter 12 November 2008
  • 2. are these the same thing?
  • 3. 1 2 radio toggleButton we have one radio with two buttons
  • 4. toggleButton toggleUp () toggleDown () toggleButtons share the same behaviour (methods) we could say they are a particular class of objects
  • 5. Class Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviours (the things it can do, or methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur colour (characteristics), and the ability to bark and sit (behaviours). source: wikipedia/Object_orientated_programming
  • 6. Javascript does not have formal classes it can approximate class based programming but it has other mechanisms for inheritance
  • 7. class-based inheritance vs. prototype-based inheritance nothing to do with the Prototype framework
  • 8. Classical inheritance ToggleButton instantiate tuningButton volumeButton function ToggleButton () { … } var tuningButton = new ToggleButton(); var volumeButton = new ToggleButton();
  • 9. Prototypal inheritance toggleButton prototype tuningButton volumeButton beget var toggleButton = { … }; var tuningButton = Object.beget(toggleButton); var volumeButton = Object.beget(toggleButton);
  • 10. toggleButton toggleUp () toggleDown () prototype Then look here tuningButton First look here tuningButton.toggleUp();
  • 11. class-based approach: you always have to create a class blueprint in order to create an object prototype-based approach: you can use an existing object as a prototype for more objects
  • 12. Object.beget = function (obj) { var F = function () {}; F.prototype = obj; return new F(); }; // myThing = Object.beget(thing); the beget method returns an object that uses the argument as its prototype source: Doug Crockford
  • 13. There is more to inheritance ...but that’s all for now!
  • 14. Review classes of objects share characteristics and behaviour Javascript supports several inheritance mechanisms Class-based (but not true classes) Prototype-based