I am reading Secrete of Javascript Ninja these days. This book introduced many js tips/patterns, and I wish to summarize what I find useful in this post.

# Force the “new” operator

JS is not that object oriented. In JS, all functions is a constructor when it is called with a “new” operator, and “this” variable in the function is bounded to the newly constructed object automatically. This was considered a harmful pattern, because if I forgot to add “new”, then the function will not be considered a constructor, and “this” variable refers to global variable. However, this situation can be avoided by adding the “new” operator automatically if we forgot. Following code does this:

In this case, “User” cannot be used as a normal function.

# Get function it self inside function execution

JS allows you to write code in functional programming style. In other functional programming languages like Scheme, it is not easy to do recursion using a lambda function, because you don’t know how to call the function it self: it got no name. In JS, you can always call current function through arguments.callee.

# Use setTimtout to help break time consuming script

If a script is running when page is loaded, then the page will only be displayed after the script finishes running. However, we can use setTimeout to break long script into small pieces. When the script is constructing page, page view update is not done immediately. Instead, it is pushed into a queue, and everything in the queue will be handled after script block finish running. setTimeout function will break the current script block, and let those tasks in the queue have a chance to be handled.

# Refer to group in regular expression

Like regular expressions in many other languages, JS use “\1” to refer to first matching group, “\2” for the second…However, this only works inside regular expression itself. One application is primality testing in my earlier post.

JS string also has “replace” method, which support regular expression replacement. To refer to matching group in the second argument of replace method, we need to use “$1”, “$2” instead of “\1”, “\2”.

# Remove empty text node from HTML

When using DOM to get html node, text nodes containing white space only is very annoying. Following code removes white space text nodes using DFS.