I just posted it because I find it easier to use than document.getElementById(), considering I use it a lot.
You shouldn't. Both the methods it is capable of using to look up an element are deprecated. You should use document.getElementById(). You may assign a reference with a shorter name if you like. However, that name should not be $.
You can declare a variable in JS whose name is $ need not be in PHP
You can, but should not. Use of the $ character in identifiers is reserved for machine-generated code (ECMA-262 section 7.6).
My own favourite utility functions are:
Code:
function map(f, a) {
for (var i = a.length - 1, r = []; i >= 0; --i)
r[i] = f(a[i], i);
return r;
}
function filter(f, a) {
if (typeof f === "string")
f = (function(f) {
return function(a) {
return !!a[f];
};
})(f);
for (var i = 0, r; i < a.length; ++i)
if (f(a[i]))
r.push(a[i]);
return r;
}
function reduce(f, a, t) {
if (!a.length)
return t;
t = t || a[0];
for (var i = 0, n = a.length; i < n; ++i)
t = f(t, a[i]);
return t;
}
function compose(f1, f2) {
return function() {
return f1(f2.apply(this, Array.prototype.slice.call(arguments)));
};
}
function bind(obj, fun, args) {
return function() {
if (obj === true)
obj = this;
var f = typeof fun === "string" ? obj[fun] : fun;
return f.apply(obj, Array.prototype.slice.call(args || [])
.concat(Array.prototype.slice.call(arguments)));
};
}
... as well as a bunch of curryable operator functions. I have a few more, nicely modularised, but they're all on my desktop, which is currently kaput. If I found myself using many of these, I suspect I would switch over to the Functional Javascript library.
Bookmarks