Syntax Tricks: Region Blocks

One thing I really miss from Visual Studio is the ability to fold entire regions of code under a meaningful description. This allowed developers to hide code they weren’t particularly concerned with at a given moment. I work on several large projects, and the lack of regions has been driving me nuts, but I think I’ve stumbled on a clever way to organise JavaScript code…

Most IDEs and text editors support code folding. The detection algorithm for code folding usually comes down to “blocks” which are defined with curly braces: { … }. For example, a multi-line if statement requires a block to be defined:

[js]
if (true) {
doStuff();
doMoreStuff();
}
[/js]

Most editors will allow you to “fold” the if statement’s block here. However, it’s perfectly legal to define arbitrary blocks almost anywhere else in your code where a statement is typically valid:

[js]
var foo = 1;
{ // start block
doStuff();
} // end block
doMoreStuff();
[/js]

We can now “fold” the block and hide doStuff();

Finally, we’re now able to hide arbitrary pieces of code in JavaScript! However, we can go even further…

Continue reading Syntax Tricks: Region Blocks

Prototypal Inheritance: One Size Does NOT Fit All

There is the prevailing attitude within the JavaScript community that prototypal inheritance will fit for all use cases where object-oriented design is required. However, in real world application development, this isn’t quite so ideal. First, let’s examine the history.

JavaScript, as a language, was designed by Brendan Eich for Netscape back in 1995. He was tasked to design a language that looked like Java, the popular programming language of that time with its promise of cross-platform compatibility. Thus, JavaScript was born… in just ten days. However, there was one problem with Java: classes. Why buck the trend and omit classes if one was trying to emulate the most popular programming language of the time? Why, when tasked with creating a familiar language, would one opt for a design that was alien to contemporary programmers?

Continue reading Prototypal Inheritance: One Size Does NOT Fit All