Here are the links to the previous installments:
- Functions
- Objects
- Prototypes
- Enforcing New on Constructor Functions
- Hoisting
- Automatic Semicolon Insertion
- Static Properties and Methods
- Namespaces
- Reusing Methods of Other Objects
- The Module Pattern
I just want to quickly share some beautiful JavaScript code I picked up while watching the most excellent screencast 11 More Things I Learned from the jQuery Source by Paul Irish.
var base = dom.getElementByTagName('base')[0] | | (function() {
// Do some stuff
return someElement.insertBefore(dom.createElement('base'), someElement.firstChild) ;
})();
This single line of code basically checks whether there’s a base tag somewhere in the DOM. If there is one, then it assigns the reference for the first element to the base variable. If it’s not in the there, then a self-executing function inserts a new base tag into the DOM and returns the reference to the new element.
I don’t know about you, but I think this is pretty neat.