What is Object Oriented Programming (OOP)?

What is Object Oriented Programming (OOP)?

My dog, Vincent

A way of representing things as collections of code with data and actions, including mechanisms for reusing code among related collections.

OOP Core Concepts

  • Abstraction

    "Abstraction is the stripping away of details so that you can see the essence." (Douglas Hofstatder)

OOP Core Concepts

  • Abstraction
  • Encapsulation

    Creating an interface for your object model and abstracting the implementation of the actions of that model.

OOP Core Concepts

  • Abstraction
  • Encapsulation
  • Inheritance

    Can you explain the relationship between two objects "A" and "B" by: "A is a B" within the code structure provided in the language?

OOP Core Concepts

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

    Literally: "many forms". The ability for a member of a collection to have different forms within the inheritance chain without changing the interface exposed outside the object.

OOP Secondary Concepts

  • Public, private, & protected
  • Static members
  • Interfaces
  • Abstracts

OOP Secondary Concepts

  • Public, private, & protected privileged
  • Static members
  • Interfaces Mixins
  • Abstracts

"I thought JavaScript was a functional language?"

(dis) Functional

So is it functional
or object oriented?

JavaScript is a multi-paradigm language.

O Rly?

But JavaScript isn't really
object oriented...

Even our functions are objects!

FYI, while undefined is clearly not an object,
null is: typeof null === "object"

Simple Objects

Using the "curly brace" syntax (object literals)...

which is equivalent to...

Calling object methods

this inside an object method refers to the current context.

Context is typically the variable before the "." when called:
this === literalDog inside speak()

Why not use object literals?

  • Limited reusability
  • More difficult for inheritance
  • Verbose
  • Poor abstraction

Constructors

A Dog by any other name...

What have we created?

Notice how our name and speak members are NOT in our prototype!

The "new" keyword

JavaScript is actually taking 3 distinct actions:

  • Create an empty object;
  • set the new object's __proto__ property to point to Dog.prototype;
  • and call the constructor method on the new object.

The "new" keyword

In fact, we can do these actions on our own:

Note that Object.create() does not exist in IE < 9, but you can use a shim.

A better Dog

What about name and speaknot being on the prototype?
We probably want them to be...

A prototypical Dog

Dun dun dun...

(That's pronounced "dunder proto", as in: "double underscore proto")

Dunder proto no more

__proto__ has existed for a long time, and in ES6/ES2015 it is deprecated

Member Access Types

public, private, privileged

Public Members

Anything attached to the prototype:

Public Members

Anything on this or a reference to the object instance:

Private Variables

And privileged methods

Private Variables

Variables declared inside a function block only exist while that function's execution context exists.

Priveleged Methods

If we want access to a private variable we have to create a "privileged" method...

Access to the alive variable from isAlive() and die() is possible because functions in JavaScript are closures.

Closures

Closure diagram (1/7)

Closures

Closure diagram (2/7)

Closures

Closure diagram (3/7)

Closures

Closure diagram (4/7)

Closures

Closure diagram (5/7)

Closures

Closure diagram (6/7)

Closures

Closure diagram (7/7)

Priveleged Methods

If we want access to a private variable we have to create a "privileged" method...

Priveleged Methods

Priveleged Methods

Static Members

Static Members

Static members are set directly on our Dog constructor function.

Static Members

Static Members

Be careful when using static methods,
you cannot access the instance (this) inside them!

Inheritance!

Inheritance

Our New Chain

Our New Chain

prototype chain

Checking instances

Polymorphism
and Parent Methods

Parent Method Declaration

Parent Method Declaration

Mixins

Interfaces

Define the Mixin

Mixing it in

We need to copy our properties and methods
from the mixin to the desired prototype...

Use a helper method!!

Mixing it in - Use a helper method!

Mix it in

Using the Mixin

The Future

ES6/ES2015 Classes

ES6/ES2015 Classes

This looks nice, but it's just syntactic sugar!

In my opinion, this will just lead to more confusion...

Don't write a framework!

There are plenty of good solutions out there...

Thank You!

Object Oriented JavaScript

Jordan Kasper

@jakerella | jordankasper.com/oop-js

See this talk in blog form!