Awkard Looking JavaScript
Ok, straight and to the point. Douglas Crockford writes about private properties and methods within the JavaScript language. Simple enough. I buy it, and, well of course, it makes sense too. There was one thing however that caught my attention. But this has nothing to do per sé with Douglas' discussion on private methods. It has to do with self-invoking functions and the similarities between how a function can be constructed.
Here is the example quoted as per Crockford:
function constructs
/* Note: The function statement */
function membername() {
// do stuff here
}
/* is shorthand for */
var membername = function() {
// do stuff here
};
Cool. Makes perfect sense. Now let's introduce some self-invoking behaviors to these two types of styles.
self-invoking function examples
(function i_am_anonymous(me) {
alert(me);
}('one'));
(function i_am_anonymous() {
alert('two');
}());
// hell, why even have a function name
(function() {
alert('three');
}());
var so_what_am_i = function(me) {
alert(me);
}('huzzah');
Ok now, first off all, self-invoking syntax is the wierdest looking syntax in the JavaScript language. That may be partly because I don't have CS degree. Nevermind the matter, take special note to the final example where we declare what_am_i as a function. Is it in the global space? Is it in an anonymous space? You sure as heck can't call who_am_i. Or wait, yes, you can... once. And you're running it in the act of declaring it. Hence forth, self-invoking.
What's all this mean?
Nothing really. I just thought I'd point it out. Nothing special. I can talk about the usefulness of closures. But I'll save that for later. So for now, just be enlightened with this knowledge (or confused). Ciao.
recent
- Matador: The Obvious MVC Framework for Node
- Sandboxing JavaScript
- Crouching Ender, hidden command
- Ender.js - The open submodule library
- Qwery - The Tiny Selector Engine
- Klass
- Smallest DOMReady code, ever.
- $script.js - Another JavaScript loader
- About that slowness on Twitter...
- Autocomplete Fuzzy Matching
- JavaScript Cache Provider
- JavaScript Animate
- Asynchronous method queue chaining in JavaScript
- Something changed
- Unofficial Twitter Widget Documentation
i am dustin diaz

