javascript - 这段代码中 (t/=d/2) 是什么意思?

标签 javascript jquery easing

jquery.easing plugin像这样的方法有很多:

easeInOutQuint: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
}

(t/=d/2)严重惹恼了 jshint!

Linting assets/js/_main.js ...ERROR
[L119:C13] E030: Expected an identifier and instead saw '='.
    if ( (t/=d/2) < 1) {
[L119:C14] E020: Expected ')' to match '(' from line 119 and instead saw 'd'.
[L119:C19] W116: Expected '{' and instead saw '<'.
[L119:C19] E030: Expected an identifier and instead saw '<'.
[L119:C19] W030: Expected an assignment or function call and instead saw an expression.
[L119:C20] W033: Missing semicolon.
[L119:C21] W030: Expected an assignment or function call and instead saw an expression.
[L119:C22] W033: Missing semicolon.
[L119:C22] E030: Expected an identifier and instead saw ')'.
[L119:C22] W030: Expected an assignment or function call and instead saw an expression.
[L119:C23] W033: Missing semicolon.

(为简洁起见,删除了重复的 JS Lint 输出行)

什么是(t/=d/2)在这里做什么?

我想修复它(而不是告诉 grunt 忽略它,我现在已经这样做了),但我不明白它在做什么。某种正则表达式?请注意td作为参数传入。算术速记?两者都?

编辑

感谢您快速给出精彩答案。将行更改为 if ( (t = t / (d / 2)) < 1)让 jshint 别再大惊小怪了。将添加答案,说明为什么 jshint/jslint 选择抛出此错误。 TL;DR:正是因为发生在我身上的事情:“这是算术还是正则表达式?”

最佳答案

这里 jsHint 是错误的。这就是

t /= d / 2

这意味着

t = t / (d / 2)

请注意,括号在替代版本中很重要,因为通常 / 运算符从左到右绑定(bind)。 /= 运算符的优先级低于 /

无论如何,整个事物的值将是“t”的结果(更新)值。

现在,从更广泛的意义上来说,jsHint 提示其晦涩难懂可能是正确的,但这是一个风格问题。运算符赋值运算符是一个相当古老的传统,至少可以追溯到 C 语言。

关于javascript - 这段代码中 (t/=d/2) 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22339266/

相关文章:

jquery - Modernizr 检查灰度滤镜 IE 10

jquery - 你如何在 jQuery 中向右缓慢滑动表格列?

javascript - 确定 snap-scroll 元素的 snap 滚动事件是否完成

javascript - 解析 RFC 2822 格式的日期字符串(在 HTTP header 字段 `last-modified` 中找到)

javascript - 如何在jqgrid中显示信息对话框设置位置中心

javascript - clientHeight/clientWidth 在不同的浏览器上返回不同的值

javascript - 可以将 HTML dom 元素拖放到 SVG dom 元素上吗?

jquery - 使用 iframe 下载文件在 Iphone/Ipad 中不起作用

jQuery easing plugIn,负scrollTop,到达顶部或底部时

javascript - 给动态 id Angular2 绑定(bind)