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:
scroll: function (a, c) {
if (!this.locked && !this.animating) {
this.pauseAuto();
this.animate(this.pos(a), c)
}
}
As for the second example, it's important to note the iteration is over the k
variable, not a
:
var a = function (a) {
i.get(a).each(function () {
h(i, this, a, b, c)
})
};
for (k = d; k <= f; k++) {
if (k !== null && !(k >= j && k <= e)) {
a(k)
}
}
For the second example, don't forget it's possible to define multiple variables in one var
statement using the ,
:
var a = 1,
b = 2,
c = 3;
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.
Because of short-circuit evaluation:
a && b && (c, d, e);
is basically equivalent to:
if (a) {
if (b) {
c;
d;
e;
}
}
And the value of the expression will be e .
In the second example, the function is not an iterator, he is just using the first position of the for loop to initialize an anonymous function and then assign it to a .
The author of that code is just being overly terse. Just because you can, doesn't mean you should.
Update :
Here is a re-write of the second example that should be functionally equivalent:
function a (a) {
i.get(a).each(function () {
h(i, this, a, b, c)
});
}
for (k = d; k <= f; k++) {
if ((k !== null) && (!(k >= j && k <= e))) {
a(k);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.