Metaprogramming JavaScript

Dan Webb (

the writing of computer programs that write or manipulate other programs (or themselves) as their data”

With JavaScript?!
★ Small and fiesty but widely misunderstood ★ Relies on few but powerful constructs ★ You can work with functions, arguments,

built-ins types and mechanisms like inheritance at runtime
★ Techniques underpin many JS libraries

JavaScript Exposed: Objects Out Of Control!

Play along!

4 building blocks

(Nearly) everything is a hash

number string boolean undefined

everything else is an Object

This works with any object

Expandos: a special case

Inspecting objects

Inspecting types


First typeof then instanceof

Functions are Objects

'Methods' are just functions that are assigned to a property of an object


Functions can return other functions


Back to wikipedia...
A closure occurs when a function is defined within another function, and the inner function refers to local variables of the outer function.

Why is that useful?

Functions can be used to make objects

Constructor functions are just functions

The magic is in the new operator


The point is: its all just functions, objects and properties

We can hack it all.

So what can you do with this stuff?

Patch holes in bad implementations

Self optimising code

Pretty APIs

Domain Specific Languages


DIY language features

John Resig's method overloading

Prototype 1.6 Classes

and a lot more...

Looking back...
★ JavaScript is small but totally flexible ★ A handful of techniques provide everything

you need
★ It's all about objects, properties and

★ Don't worry if its not all sunk in. Have a

play around. Slides will be available.

Coming Soon...

Sign up to vote on this title
UsefulNot useful