One-shot Getters & Setters

The last hour was spent exhaust coding some broadcast message bits with socket.io. In the middle of this I wrote a small function that reassignes itself after it is called; a one-shot setter.

Expanding slightly on this a little bit I wrote two generic gists demonstrating this in getter and setter fashion.

Getter Example

Here we simply replace the function with another function that returns the initially given value.

Setter Example

The setter example is a bit more complex. In order to correctly scope the passed function we create a copy of it, substituting the current context.

This context can even be altered externally by binding this, ala error.bind(context)(handler);.

written August 30th, 2011

August 2011

Can’t find what you’re looking for? Try hitting the home page or viewing all archives.