JavaScript怎么可以逻辑和赋值乱用,看不懂啊

chenfeibiao328 2011-08-07 04:06:04

index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);

出自这个方法:

Run: function(index) {
//修正index
index == undefined && (index = this.Index);
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
//设置参数
this._target = -Math.abs(this.Change) * (this.Index = index);
this._t = 0;
this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);
this._c = this._target - this._b;

this.onStart();
this.Move();
}


谢谢啊,急
...全文
106 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenfeibiao328 2011-08-07
  • 打赏
  • 举报
回复
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
噢,谢谢啊,明白了
乌镇程序员 2011-08-07
  • 打赏
  • 举报
回复
相当于:
if (index < 0) index = this._count - 1;
else {
if (index >= this._count) index = 0;
}


这是利用了运算符的优先级、结合性(具有相同优先级的运算符按从左至右的顺序求值)和逻辑与、逻辑或运算的简便运算特性,这里涉及到的优先级(高到低):
< >=
&&
||
=

简便运算特性是指:
&&运算时,如果第一个运算数值为false,不再计算第二个运算数值
||运算时,如果第一个运算数值为true,不再计算第二个运算数值
MuBeiBei 2011-08-07
  • 打赏
  • 举报
回复
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);


就是一个赋值啊``

如果index小于0就执行(index = this._count - 1)

如果index >= this._count 就执行 index = 0

87,917

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧