简体   繁体   English

任何人都可以从jCarousel解释以下JavaScript代码吗?

[英]Can anybody explain the following JavaScript code from jCarousel?

I've been writing JS for a while, and I have been trying to debug the jQuery jCarousel plugin for IE7 compatibility. 我已经写了一段时间的JS,我一直在尝试调试jQuery jCarousel插件以兼容IE7。 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 : 至于第二个例子中,要注意的迭代过很重要k变量,而不是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语句中定义多个变量

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. 当谈到发布时间时,他将通过缩小UglifyJSClosure Compiler等)运行缩小版本以获得简化代码。

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 . 并且表达式的值将是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 . 在第二个例子中,函数不是迭代器,他只是使用for循环的第一个位置来初始化一个匿名函数,然后将它分配给一个

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM