SlightlyLoony
Tera Contributor
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
03-28-2011
06:53 AM
If you write even a few JavaScript programs, you're going to run into situations where you need to make a test, then take some action only if the test had a certain outcome. Probably the most common example of this is testing to see if a reference is null before reading a property or calling a method on the object referred to. For example, suppose your program had a reference "x" to an object (but it might be null or undefined), and that object had a method "y" that you'd like to call if it is defined (and that depends on what type of object "x" was. Here's how you might code that:
if (this.x != null) {
if (typeof x.y == 'function')
x.y();
}
Ah, but there's a more concise way, using a feature of the JavaScript language called "short circuit evaluation". That's not a reference to what happened to our girl's hair (at right), but rather to a special way that certain operators work. Here's what the code above would look like, rewritten to use short circuit evaluation:
(this.x != null) && (typeof x.y == 'function') && x.y();
What's going on here? Let's pick it apart...
The short circuit evaluation magic is happening on the three sub-expressions separated by the "&&" operators. These sub-expressions are always evaluated left-to-right, but the evaluation of the sub-expressions stops as soon the result of the overall expression can be determined. That sounds complicated, but it's really not. Imagine, for instance, that "x" was null when this code was evaluated. The first sub-expression evaluates to false — which means that no matter what the other sub-expressions evaluate to, the overall expression's result will be false. To obey the special short circuit evaluation rule, that means the second and third sub-expressions will not be evaluated — and that's a good thing, because when "x" is null, they'd both fail big time! Something very similar happens in the second sub-expression if "x" is not null, but it doesn't have a "y" function. In that case the third sub-expression will not be evaluated — and the invocation of "x.y()" will not fail.
In JavaScript there are two operators for which short circuit evaluation applies: "&&" (logical "and") and "||" (logical "or"). It's worth learning both of these well, and what makes them different than the very similar (but not short circuit evaluating!) bitwise integer operators "&" and "|".
There's another (unrelated) piece of that example that's worth noting: it tests "this.x != null" instead of "x != null". Do you know why? In my example, I'm assuming that "x" is either a global variable or an instance variable (i.e., a property in the current object). By including the "this.", the test will work correctly even if "x" is undefined; otherwise the test would cause a runtime error.
Here's one more example, this time of probably the most common usage pattern for short circuit evaluation: making sure an object isn't null before you attempt to retrieve a property or call a method from it:
var x = getMyX();
if ((x != null) && x.valid()) {
// do important stuff because x is valid...
}
This is a pattern you'll see frequently in our code, and it's a good example of handy-dandy short circuit evaluation at work...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.