Exercise one

Here’s some JavaScript code I found somewhere. I’ve changed the names of everything to hide the origin of the code. But it’s not something I’ve made up.

The exercise is

  1. Understand and describe what it does.
  2. Provide clearer code that does the same thing.
  3. Suggest a better way of going about things.
var AAA = (function (BBB)
    BBB.fff = function(ccc)
        return {
            ggg: function(ddd)
                return BBB.hhh(ddd, ccc.iii());
    return BBB;

}(AAA || {}));


  1. The code creates and calls an anonymous function, a bit like this.

    js> a = function(x){return x + 1}(2)
  2. What happens if AAA is an object?

  3. What happens if AAA is undefined?

  4. For now, ignore the assignment to BBB.fff. What value does AAA get? (The answer depends the initial value of AAA.)