I’ve been writing JS for a while, and I have been trying to debug the jQuery jCarousel plugin for IE7 compatibility. A lot of the variables are obfuscated, which is OK, but I have never seen syntax like this:
scroll: function (a, c) {
!this.locked && !this.animating && (this.pauseAuto(), this.animate(this.pos(a), c))
}
It seems like some sort of shorthand notation, but I’ve never come across anything like this.
Similarly,
for (var a = function (a) {
i.get(a).each(function () {
h(i, this, a, b, c)
})
}, k = d; k <= f; k++) {
k !== null && !(k >= j && k <= e) && a(k)
}
I have never seen a function being assigned as the iterator, and again the block statement inside looks like the first example. I know we all strive to save a few bytes when we write our code, but to me this feverish attempt comes at a cost of utter confusion and bewilderment to other programmers. Can anybody can give me a simple, “longhand” alternative / thorough explanation of what’s happening? I always want to know more about this language and how it works, thanks.
The
&&operator evaluates each expression until it finds one which is truthy, which it returns. If none are truthy, it returns the last one.The
,operator evaluates both it’s operands and returns the second one (no matter what they return).So in the first example, it’s basically saying:
As for the second example, it’s important to note the iteration is over the
kvariable, nota:For the second example, don’t forget it’s possible to define multiple variables in one
varstatement using the,:It’s important to know that the developer himself isn’t minimizing the code like this; he’s writing normal source code with meaningful variable names in easy to read blocks. When it comes to release time, he’ll run the minified version through a minifier (UglifyJS, Closure Compiler etc) to get the reduced code.