Objects, Functions and Inheritance...

I love javascript!

I love javascript!

I love javascript!

I love javascript!

Javascript annoys me!

So this rant happened

Who's responsible?

Let's play


Objects, Functions, Inheritance

Object Based, not -Oriented

* I am going to retract this statement in about 10 slides...

Objects are Dictionaries

Functions

Functions and Objects

OK OK but let's use them as objects already


Hint: It's gonna get rough

It Just Works!

Function invocation and "this"

Function invocation and "this"

Mistake: "this" to global

Fix it with a feature

Inheritance


Oh boy, oh boy, oh boy!

An issue from before:

Hide it in the scope?

duck typing

but we do care about

Prototypal inheritance

Prototypal inheritance

Prototypal inheritance

everything is broken!

MORE broken!

Constructor Functions

Constructor Functions

The intent is not bad:

4th Function invocation:

Constructor sets __proto__

Let's try inheritance

in diagram form

			function Animal() {}
			Animal.prototype.makeNoise = function() { return this.noise; }

			function Duck() { this.noise = "quack"; }
			Duck.prototype = new Animal();

			function Dog() { this.noise = "woof"; }
			Dog.prototype = new Animal();

			var duck = new Duck();
			var dog = new Dog();
	

in diagram form

			function Animal() {}
			Animal.prototype.makeNoise = function() { return this.noise; }

			function Duck() { this.noise = "quack"; }
			Duck.prototype = new Animal();

			function Dog() { this.noise = "woof"; }
			Dog.prototype = new Animal();

			var duck = new Duck();
			var dog = new Dog();
	
WHAT THE?! SO WHY DO WE EVEN HAVE CONSTRUCTOR FUNCTIONS?! THEY REALLY SHOT THEMSELF IN THE FOOT WITH THIS ONE!

pseudoclassical

alternatives?

Can we fix this?

At least the interface?

Still kinda sucks

options?

My way

* makes it easy to decide where code should live (Classes, not duplicated)

my way

One ugly part left :-(

how?

cool huh? not yet!

Recap

ECMAScript 6

 

 

Thank you!

github.com/njoubert/inheritance.js