Posts tagged with 'module'


JavaScript for C# developers: the Module pattern (part 4) – debugging

(For background, please check out parts 1 , 2 , and 3 before reading this post.) One of the problems about private local variables inside closures is that you can’t see them. Well, duh, you might say, that’s what private means after all. Point taken, but there’s one scenario where it would be really nice to be able to check those enclosed (enclosured?) variables: debugging. Now, I’m not going to get into the whole argument here about debugging versus testing. I’m of the firm belief that you should...

READ MORE

JavaScript for C# developers: the Module Pattern (part 3)

Now that we’ve seen the simple module pattern as well as ways to augment it , we should take a look at one final piece of the puzzle. Private fields, as we saw in the last installment, can be a real issue. Sometimes, dammit, we’d just like to refer to that private local variable when we’re augmenting a module object. Just a peek you understand, and we’d make it private again immediately we’re done augmenting, so that the code using the module object doesn’t see...

READ MORE

JavaScript for C# developers: the Module Pattern (part 2)

Last time I talked about the simple module pattern . This is where you create a function that returns an object with behavior and state and that behavior and state is implemented (and made private) by using a closure. We showed this by using the module pattern to create a stopwatch object. Let’s now see how we can extend this stopwatch object by adding the facility to have lap times. This gives us the ability to use the same stopwatch to time a sequence of time-consuming actions, rather than creating...

READ MORE

JavaScript for C# developers: the Module Pattern (part 1)

If you recall, JavaScript closures are an extremely powerful concept in the language. By using JavaScript’s rather peculiar scoping rules, closures are a way of creating private variables and functionality for an object. The module pattern builds upon this feature. A quick recap on scope in JavaScript may be in order. Scope is the means by which programmers limit the visibility and the lifetime of their variables and parameters. Without scope, all variables would be global and visible everywhere...

READ MORE