📚 Buy on Bookshop

Secrets of the JavaScript Ninja

An interactive explorer of John Resig's advanced JavaScript techniques. Write code, run it, see it visualized.

Start Exploring ↓
Chapter 1 of 7

🔒 Closures & Scope Chains

A closure gives a function access to variables from its enclosing scope, even after that scope has finished executing. This is the foundation of private variables, callbacks, and most patterns in JavaScript.

Run the code below and watch the scope chain diagram update to show which variables each function can see.

▸ Scope chain diagram — click Run to visualize
Next: Prototypes & Inheritance →
Chapter 2 of 7

🧬 Prototypes & Inheritance

Every JavaScript object has a hidden [[Prototype]] link. When you access a property, the engine walks up this chain until it finds it (or hits null). This is how inheritance works — no classes needed.

Run the code and see the prototype chain visualized as connected nodes.

▸ Prototype chain — click Run to visualize
Next: Function Context (this) →
Chapter 3 of 7

🎯 Function Context: this

The value of this is determined by how a function is called, not where it's defined. This is one of the trickiest parts of JavaScript. There are four rules: default, implicit, explicit (call/apply/bind), and new.

Run the code to see which object this points to in each context.

this binding diagram — click Run to visualize
Next: Timers & The Event Loop →
Chapter 4 of 7

⏱️ Timers & The Event Loop

JavaScript is single-threaded. Timers don't execute at exact times — they queue a callback for "at least" that delay. Resig's key insight: setTimeout chains guarantee a gap between executions; setInterval fires regardless of whether the previous callback finished.

Next: Regular Expressions →
Chapter 5 of 7

🔍 Regular Expressions

Resig calls regex one of the most underused features in JavaScript. Key techniques: pre-compilation for performance, exec() in a loop for global search with captures, and replace with a function for powerful transformations.

Next: Runtime Code Evaluation →
Chapter 6 of 7

⚡ Runtime Code Evaluation

JavaScript can generate and execute code at runtime — via eval(), new Function(), and dynamic <script> tags. Resig shows this isn't just a footgun — it powers JSON parsing, templating engines, and even entire programming languages (Processing.js).

new Function() creates a function without closures — cleaner and safer than eval.

Next: Cross-Browser & DOM →
Chapter 7 of 7

🌐 Cross-Browser & DOM Mastery

Resig's #1 rule: test the feature, not the browser. Use object detection and feature simulation instead of user-agent sniffing. Bugs get fixed — your workarounds should survive.

Try DOM manipulation with the live playground below. The preview updates in real time.

▸ DOM preview — click Run to render
↓ The Way of the Ninja

The Way of the Ninja

"There is nothing simple about creating effective, cross-browser, JavaScript code. But understanding how the very best libraries are constructed can provide great insight into how your own code can be constructed."

— John Resig

Every editor above is live. Modify the code, run it again, break things, fix them. That's how ninjas learn.