background preloader

JavaScript

Facebook Twitter

Tal Bereznitskey - Javascript Hacks for Hipsters. Javascript is so much fun, except when it’s not.

Tal Bereznitskey - Javascript Hacks for Hipsters

There’s always the fear of runtime errors that keeps us thinking all the time while writing code. It makes us better coders - we have no other option than to visualize every line of code as if it’s running as we write it. That’s why it’s so important to have tidy code. Small code. Pretty code. I gathered some fun snippets I enjoy using instead of boring code that takes too much space. I learned all of this from open source code (until node.js all javascript code was open source, wasn’t it?) Hipster Hack #1 - Method calling I really hate if/else blocks and this is quite a useful trick to call the right function based on a boolean value.

Hipster Hack #2 - String joins It’s a known fact that strings like other strings. Hipster Hack #3 - Default Operator || Javascript is all about not knowing what an object holds. Hipster Hack #4 - Guard Operator && Similar to the Default Operator, this one is super useful. Hipster Hack #5 - XXX Operator.

Performance

Google JavaScript Style Guide. We follow the C++ formatting rules in spirit, with the following additional clarifications.

Google JavaScript Style Guide

Curly Braces Because of implicit semicolon insertion, always start your curly braces on the same line as whatever they're opening. For example: if (something) { // ... } else { // ... } Array and Object Initializers Single-line array and object initializers are allowed when they fit on a line: Multiline array initializers and object initializers are indented 2 spaces, with the braces on their own line, just like blocks. Long identifiers or values present problems for aligned initialization lists, so always prefer non-aligned initialization.

Not like this: Function Arguments When possible, all function arguments should be listed on the same line. // Four-space, wrap at 80. When the function call is itself indented, you're free to start the 4-space indent relative to the beginning of the original statement or relative to the beginning of the current function call.

Google Maps

Testing. Design Patterns. Raphael.js. Code and Examples. OO JS. $.Deferred. Javascript Closures. Introduction Closure A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).

Javascript Closures

Closures are one of the most powerful features of ECMAScript (javascript) but they cannot be property exploited without understanding them. They are, however, relatively easy to create, even accidentally, and their creation has potentially harmful consequences, particularly in some relatively common web browser environments. To avoid accidentally encountering the drawbacks and to take advantage of the benefits they offer it is necessary to understand their mechanism. This depends heavily on the role of scope chains in identifier resolution and so on the resolution of property names on objects. The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. Assignment of Values Finally:-

Memory leaks

Node.js. Build Library. Motion.