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. :)
My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...
I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...
As broadband speed continues to get faster, the web continues to be more media-centric. Sometimes that can be good (Netflix, other streaming services), sometimes that can be bad (wanting to read a news article but it has an accompanying useless video with it). And every social service does...
Yesterday I created a tutorial showing you how you can shake an element using Fx.Shake, a MooTools component written by Aaron Newton. It turns out that jQuery UI also has a shake effect which can draw attention to an element.
The XHTML
Exactly the same as...
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.