11. What Is in the Resolution Chain?
Functions dictate scope (with a few exceptions)
• Local variables
• Arguments
• Enclosing function scopes
• With Statement
• Global
12. Each Step Is Like a Property Access
locals.arguments.scope1Locals.scope1Args.globals
16. The Power of Closures
With great power comes great responsibility
• Reduce typing
• Make code cleaner
• Provide some privacy
• Sometimes they make things fast
• Magic (AKA functional)
23. Using Objects Effectively
• First, do what makes sense—don’t over-optimize
• Every property access is slow
• Breadth not depth
• Use object literals
• Make shortcuts
• Inspectable
27. DOM Tips
• Watch out for closures referencing nodes
• These can leak
• Reference nodes anyway
• Using a reference is much faster than looking up a node
• innerHTML is generally faster than the DOM API
No block scope. In general you reference enclosing functions and their arguments until you reach the global scope. With is an exception.
This isn’t as true in v8.
Referencing a is 4 steps away!
If something isn’t defined in the object in your with statement, you create a global. If it is, you overwrite that property. It’s difficult to tell from reading the code how an assignment might affect the program.
And you can combine the two!
This avoids a step on the scope chain.
Minimize your code, don’t keep functions around that are one-offs. On the other hand, this function is recreated every time.
As always with closures, watch for memory leaks.
Be careful though, unlike other languages with getters and setters, Javascript won’t warn you if you access an undefined property. It’ll just return undefined.