WordCamp Nashville 2016

34
bobby | @mrbobbybryant #wcnash Javascript Prototypal Inheritance. How its different, and why you need to know it Bobby Bryant

Transcript of WordCamp Nashville 2016

Page 1: WordCamp Nashville 2016

bobby | @mrbobbybryant #wcnash

Javascript Prototypal Inheritance.

How its different, and why you need to know itBobby Bryant

Page 2: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

About Me

• Web Engineer at 10up

• Father of 3

• JavaScript Junkie

Page 3: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Why Cover such a weird topic?

Page 4: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

–Kyle Simpson @getify

“Where it takes a pretty in-depth knowledge of a language like C or C++ to write a full-scale

program, full-scale production JavaScript can, and often does, barely scratch the surface of

what the language can do.”

https://github.com/getify/You-Dont-Know-JS

Page 5: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Classical vs Prototypal Inheritance

Page 6: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Classical Inheritance• Allows you to create new objects using a class as a blueprint.

• A blueprint as a few characteristics that make it distinct.

• It implies that if you follow the instruction you will successfully create your object.

• Class Based languages are built around the concept of:

• Building base classes.

• Extending the base classes to build a hierarchy of inheritance.

Page 7: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Classical Inheritance

• Class Based languages are built around the concept of:

• Building Base Classes.

• Extending the base classes to build a hierarchy of inheritance.

Page 8: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Prototypal Inheritance• A prototype is one step farther than a blueprint.

• It implies that you have a working version of something.

• It not a contract that says if you follow these steps you will have a working object.

• It implies that, “here is a working object, use it as is or make copies if you want.”

Page 9: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

How Does JavaScript use these prototypes?

Page 10: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Page 11: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Lots of them

Page 12: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Understanding the Prototype

Page 13: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Prototypal Inheritance

Page 14: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Prototypal Inheritance

Page 15: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Static Methods

Page 16: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Real World Example

Page 17: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Constructor

Page 18: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Extending a Prototype

Page 19: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Object Literal

Page 20: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Delegation

Page 21: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Delegation

Page 22: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Object Creation Patterns

Page 23: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Object.create

Page 24: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Factory Function

Page 25: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Factory Fn #2

Page 26: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Composition vs Inheritance

Page 27: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Currently in the larger JS world, there is actually a completely different paradigm growing. Functional Programming. And at its core

functional programming is about Composition over Inheritance.

Page 28: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Object.assign

Page 29: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Object.assign

Page 30: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Composition

Page 31: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

ES6 Classes

Page 32: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Is class syntax in ECMAScript 6 just a syntactic sugar for classical prototypes?

https://www.quora.com/Is-class-syntax-in-ECMAScript-6-just-a-syntactic-sugar-for-classical-prototypes

Page 33: WordCamp Nashville 2016

Bobby Bryant | @mrbobbybryant #wcnash

Delegation again

Page 34: WordCamp Nashville 2016

bobby | @mrbobbybryant #wcnash

Thank You!I have a Javascript Problem.