JavaScript Classes and Inheritence
PRO Edition
Marc Heiligers • @marcheiligers
but i have a
Microsoft
background
Thank-you
for sponsoring this great venue
Thank-you
BBD, Entelect and Amazon Development Center
If you'd like to come to Port Elizabeth, maybe we can get something going there?
Back to our regularscheduled programming...
Commodore 64 BASIC
Commodore 64 Assembly
GW BASIC
Turbo Pascal
Turbo C
Java
Classic ASP
Sun Spark Assembly
GNU and MS C++
Classic ASP
<%@ Language="JavaScript" %>
Server-side JavaScript in the 90's
JavaScript
Looked like C and Java
Ran in a browser
And on the server
Wasn't VB
Not here to talk about me
What are classes?
Or at least, what do we care about?
When I lie to you, I'll mark it with a †
We care about Classes that...
- define reusable properties and methods
- can inherit functionality from a superclass
- can override functionality of their superclass
- can call their superclass methods
- are open (can be Monkey-patched)
(that last one, from a Rubyist perspective)
JavaScript
But it is possible to treat JavaScript "classes" very much like classical
classes while also taking advantage of some of the dynamic nature of JS.
Objects
Everything in JavaScript is an object†.
Strings are objects.
Numbers are objects.
Functions are objects.
And Objects are objects.
Object Properties
Key → Value
- Keys are strings
- Values can be anything:
- A number or a string or other type
- Another object or array (which is an object)
- Or a function (method)
Inherited Object Properties
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Inheritance
- __proto__†
- [[prototype]]
-
Functions are constructors
-
new Object() is calling a function
- with this special bit of syntactic sugar new
new Keyword
- creates an {}
- {}.__proto__ = Constructor.[[prototype]]
- Constructor.call({}, arguments)
-
return {}†
Classes
Constructor.prototype.method =
function() { ... }
Can also override methods
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Monkey-patching
- Constructor.prototype.method = function() {}
-
All instances of the class will get the method
- You can monkey-patch built in classes too
- Object
-
HTMLElement, HTMLDivElement, ...
- MooTools and Prototype
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Inheritance
- function Pig() {}
- Pig.prototype = new Animal()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Super class methods
Functions are Objects
- Function#call
- Function#apply
- Super.prototype.method.call(this);
Array.prototype.slice.call(arguments);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Privacy
- Nothing is private
- _method
- __method
- __method__
IIFE
- Immediately-Invoked Function Expression
- Pronounced Iffy
- Often incorrectly named SIF
- (function(global, undefined) {})(this)
- Everything is private
- Unless you make it public
More uses for IIFEs
- Pass more interesting things in
- Mixing up multiple libraries?
- (function($) {})(jQuery)
Gotcha
In strict mode, this is not coerced to the global object
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Functions
- First class
- apply
- call
- bind
First Class Functions
- Pass functions as arguments
- Return functions from functions
- Assign them to variables or in data structures
- Have anonymous functions
Closures
A closure is a function that refers to an independant variable.
Eisbein IFFE total and cook referred to count.
Binding
- bind
- $el.on('click', this.onClick.bind(this))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
What if we could create a Class construct
that would make all of this easier?
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Other options
- CoffeeScript
- TypeScript
- ES6
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Are you using something like Traceur to transpile ES6?
Or using CoffeeScript or Typescript or something like that?
Nothing at all.
Juriy "kangax" Zaytsev