Metaprogramming JavaScript

100 %
0 %
Information about Metaprogramming JavaScript

Published on November 21, 2007

Author: danwrong



As presented at @media Ajax in London on the 20th November 2007.

Metaprogramming JavaScript Dan Webb (

What? “ 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 functions ★ Don't worry if its not all sunk in. Have a play around. Slides will be available.

Coming Soon...

Add a comment

Related pages

Metaprogramming Fun In JavaScript -

The idea is to take a JavaScript statement like the following: c.find({ x: 1, y: 3, name: "foo" }, { id: 0 }).limit(1); .. and turn the statement into a ...
Read more

is metaprogramming possible in Javascript? - Stack Overflow

Is metaprogramming possible in javascript?? During my routine work, i happened to write the chained javascript function which is something like LINQ ...
Read more

Javascript Metaprogramming - Stack Overflow

Dynamic Parameters for metaprogramming in JavaScript? 2. a bit of javascript metaprogramming & chainable setters. Hot Network Questions
Read more

JavaScript Function Metaprogramming « null program

The opfuncs function should be called exactly once. These functions shouldn’t be generated multiple times because the benefits of the metaprogramming ...
Read more

Metaprogramming Javascript -

Metaprogramming, writing code that augments itself at runtime, is a very powerful technique gaining popularity in scripting languages like Python and Ruby ...
Read more

Metaprogramming JavaScript - Dan Webb

Metaprogramming... The writing of programs that write or manipulate other “ programs or themselves. ”
Read more

Metaprogramming with JavaScript | Jeremy D. Miller

About Jeremy Miller Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT ...
Read more

metaprogramming-javascript.js · GitHub

Clone via HTTPS Clone with Git or checkout with SVN using the repository's web address.
Read more