Memoization is one nice trick you can use to improve your JavaScript code. The idea is quite simple: in scenarios where you have “pure” functions, you can improve the total time of execution by saving the results of previous executions. I think that an example will help understand how things work. Let’s start by looking […]
September, 2009Archive
Today we’ll keep looking at the new MS AJAX library (preview 5) and we’ll start looking at the observer pattern. Wikipedia defines the observer pattern like this: The observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, […]
One of the things people don’t do (most of the time) is use variables. For instance, here’s a snippet which shows some code which looks good: function do1() { var time = (new Date).getTime(); for (var i = 0; i < document.getElementById("content").childNodes.length; i++) { var txt = document.getElementById("content") .childNodes[i].innerText; //do something } var end […]
Simplifying the API of your JS methods
If you’ve been using JavaScript for some time, I’m positive that you’ve seen JS methods which look like this: function doSomething(address, methodName, shouldThrowOnError, successCb, errorCb) { //code goes here } In my opinion, this kind of API sucks. If you’re not seeing why, then suppose that all parameters are optional. When you only need to […]
Stop copying blank lines in VS
One of the things I end up doing sometimes in Visual Studio is copying blank lines. It’s not that I want to copy a blank line…it’s simply that I get interrupted while using ctrl+c/ctrl+v and end up messing things when I return to the desk. Since I’ve just ended up copying another blank line, I’ve […]
I know that it’s been a long time since I’ve used ASP.NET AJAX (I think that I didn’t even realized that it had been so long since I’ve used in my day to day development). Today, I’ve just noticed that you no longer need to invoke the notifyScriptLoaded method to notify the end of the […]
The script dependency registration of MS AJAX
I’ve finally started looking at the latest release of the ASP.NET AJAX library (preview 5). This version introduces several new features and I’ve thought I should write a couple of posts about it here. Today I’m going to talk about a feature I like to call “script dependency registration” (btw, it has been a long […]
One of the things you probably need to do is concatenate strings. I guess most of us simply use the + operator for that, right? I’ve seen lots of code which look like this: var s = "Hello World"; for (var i = 0; i < 100000; i++) { s += "something else"; }; If […]
Since closures are so great, I should use them whenever possible, right?
A colleague of mine asked me this question this week and I thought that it would be a nice topic for a post. Don’t worry, I’ll be really quick about it! Instead of saying no, I told him: it depends. “Depends on what?”, you ask. Well, one of the problem with closures is that it […]
The with statement
When I first noticed the with statement, I thought I had discover the “holly grail” of code simplification in JS. Take a look at the following snippet: var student = { name: "luis", address: "fx", hobbies: ["computers", "soccer", "tv"] }; with (student) { name = "john"; address = "lx"; } Well, everything looks good, right? […]
Book review: Portugal, que futuro?
Generally, I tend to review English written books in this blog. However, and since we’re on the verge of choosing a new PM for the next four years, I’m reviewing this fantastic book here and I’ll do it in Portuguese. In practice, this means that if you don’t understand Portuguese, you can safely skip this […]
typeof vs instanceof
Both of these operators are used to check if an object is of specific type. So, which one should you use? The answer is: it depends…I guess that none of them is perfect. typeof will always return a string which identifies the “current type” (and this info is really useless most of the time). On […]
SpriteMe released
I’ve just seen the announcement on Steve Souders’ blog (yes, I know: I’m late, but don’t forget I’ve been busy with network security training).
JavaScript logical operators
JavaScript has three logical operators: &&, || and !. We’ve already seen two posts on the ! and today we’ll wrap our discussion on logical operators by talking about the && and the || operators. Traditionally, you’ll hear something like this when these operators are presented: The && operator returns true when all the conditions […]
In one of my latests posts on JavaScript, I’ve tried to present a technique known as boolean normalization. The idea is to use the ! operator twice in order to convert any value to a boolean. At the time, instead of relying in a simple expression (ex.: var t = !! something), I’ve opted for […]
Book review: JavaScript, the good parts
I’ve just finished reading Douglas Crockford’s book and I can tell you that is a fantastic book. Notice that the book concentrates on the language itself, so don’t buy this book and expect to find a good reference for learning DHTML. The book is full of small nuggets which will guide you through the most […]
One of the things you’ll need when you start using JavaScript is iterating through the arguments object. Most of the time, you’ll probably see code which looks like this: function iterate1() { for (var i = 0; i < arguments.length; i++) { alert(i + ":" + arguments[i]); } } The previous snippet is simple and […]
I’m assuming that you’ve already seen code which looks like this: function doSomething(isTrue) { if (!!isTrue ) { //some code here } } And I’m sure you’re wondering what’s going on there. This is sometimes called boolean normalization and its main advantage is that you end up getting a true boolean value instead of relying […]
Another excellent post by Dave Reed on a topic which I shall begin looking at in depth next week.
Book review: How would you move Mount Fuji
This was another of the books I’ve managed to read during my last vacations. It’s an interesting book which presents several puzzles (supposedly) used by Microsoft during their hiring process. Besides presenting the answers to the puzzles, author William Poundstone adds a couple of “extra” chapters which try to “justify” the reasons associated with the […]
Arrays in JavaScript – part III
Today we’re going to talk a little more about arrays in JavaScript. In previous posts, we’ve already seen that JavaScript doesn’t really support “true” arrays; instead, it introduces an object which behaves like an array. This means that you can use integers as keys for accessing values (internally, the object will convert the integer into […]
In the previous post we’ve started looking at arrays in JavaScript. At the time, we’ve seen that you could initialize a new array by using the constructor or by using the literal syntax. As Daniel pointed out in the comments of the previous entry, there are several ways to initialize an array through its constructor. […]
Arrays in JavaScript
If you’ve been doing programming for some time, you’ve probably used arrays. Traditionally, arrays are linear allocations of memory where elements are accessed through indexes. In other words, in “traditional” programming languages, when you create an array, you’re allocating a chunk of contiguous bytes which is big enough to save the specified number of elements. […]
I guess this says it all. I’ve had similar experiences in the past and I still think that bing is far far far away from bing…
2011-01-12 13:36:56
OO advanced options in JavaScript – part III
In the previous posts, we’ve started looking at several important details associated with “emulating” classical OO programming in JavaScript. Today we’ll keep looking at this pattern and we’ll see how we can emulate method overloading. Before going on, I’d like to make a point about the “abuse” you’ll be seeing in the following snippet. As […]
OO advanced options in JavaScript – part II
In yesterday’s post we’ve started looking at how JavaScript “emulates” OO concepts available on “pure” OO languages. I’ve ended the post asking if you could detect the nasty side effect which will happen when you set the prototype property of a new “type”. If you haven’t seen it, then try to run the following code: […]
One thing most people don’t know is that they NaN and undefined are just global variables. Don’t believe me? Ok, run the following snippets: var initialNaN = NaN; NaN = 10; alert(NaN === 10); alert(initialNaN === 10); I can tell you that the first time someone showed me a similar example I just thought “WTF […]
Understanding when expressions are true or false
If you’ve done some C programming in the past, then you shouldn’t have any problems with understanding this concept. In JavaScript, things are really easy. Any expression which isn’t in the next list is *always* true: false null undefined empty string Zero (0) NaN In practice, this means that the following snippet will always show […]