JavaScript: Constructor Auto-Execution with new Keyword
JavaScript is full of small, interesting facets that can trip you up, make you laugh, or make you cry. This post is about an interesting one. Those of you that have worked with JavaScript functions, and in a way JavaScript "classes" (as you used with MooTools), you're well acquainted with the new
keyword. With the new
keyword you get the ability to pass arguments with the function call, but did you know that if you have no arguments, you don't need the parens at all?
function MyClass() {
console.log('Initialized!');
//Set a property, as an example
this.dirty = true;
}
var instance = new MyClass;
// >> "Initialized!''
So why am I telling you this? I have no idea. It's just one of those fun tidbits that you can add to your brain. :)
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...
Counters. They were a staple of the Geocities / early web scene that many of us "older" developers grew up with; a feature then, the butt of web jokes now. CSS has implemented its own type of counter, one more sane and straight-forward than the ole...
I recently received an email from a reader who was really impressed with Block Clickable, a jQuery script that took the link within a list item and made the entire list item clickable. I thought it was a neat script so I...
Amazing how the obvious is sometimes hidden in plain sight. I was going to save the extra () characters but it looks like there’s a micro performance hit in V8.
http://jsperf.com/new-with-and-without-parens
Really? That’s ironic, since Google Closure Compiler actually removes the parentheses when they can be omitted.
That was something I always was curious about but never bothered to ask/look up. I assume the same thing also occurs in php?
I’ll file this next to optional semi-colons and optional closing tags in HTML5: things that are interesting to know, but if I ever see while reviewing someone’s code I might get stabby.