Metaprogramming JavaScript

69,567 views

Published on

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

Published in: Technology
8 Comments
158 Likes
Statistics
Notes
No Downloads
Views
Total views
69,567
On SlideShare
0
From Embeds
0
Number of Embeds
1,368
Actions
Shares
0
Downloads
1,884
Comments
8
Likes
158
Embeds 0
No embeds

No notes for slide

Metaprogramming JavaScript

  1. Metaprogramming JavaScript Dan Webb (dan@danwebb.net)
  2. What? “ the writing of computer programs that write or manipulate other programs (or themselves) as their data”
  3. 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
  4. JavaScript Exposed: Objects Out Of Control!
  5. Play along!
  6. 4 building blocks
  7. (Nearly) everything is a hash
  8. number string boolean undefined
  9. everything else is an Object
  10. This works with any object
  11. Expandos: a special case
  12. Inspecting objects
  13. Inspecting types
  14. Wha?!
  15. First typeof then instanceof
  16. Functions are Objects
  17. 'Methods' are just functions that are assigned to a property of an object
  18. arguments
  19. Functions can return other functions
  20. Closure
  21. 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.
  22. Why is that useful?
  23. Functions can be used to make objects
  24. Constructor functions are just functions
  25. The magic is in the new operator
  26. inheritance
  27. The point is: its all just functions, objects and properties
  28. We can hack it all.
  29. So what can you do with this stuff?
  30. Patch holes in bad implementations
  31. Self optimising code
  32. Pretty APIs
  33. Domain Specific Languages
  34. DOMBuilder http://danwebb.net/lowpro
  35. DIY language features
  36. John Resig's method overloading http://ejohn.org/blog/javascript-method-overloading/
  37. Prototype 1.6 Classes http://prototypejs.org/learn/class-inheritance
  38. and a lot more...
  39. 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.
  40. Coming Soon...

×