javascript中的minus等于-是什么意思? [英] minus equals in javascript - What does it mean?

查看:167
本文介绍了javascript中的minus等于-是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-=以下的负号是什么意思/做什么?

What does the minus equals below -= mean/do?

$('#wrapper').animate({
    backgroundPosition: '-=2px'
})();

谢谢

推荐答案

Adil回答了这个问题,但我始终认为可视化问题并将其与其他人联系起来很有用.

Adil has answered this but I always think it is useful to visualise problems and relate them to others.

以下两段代码具有相同的效果:

The following two pieces of code have the same effect:

var a = 20;
a = a - 5;

var a = 20;
a -= 5;

在两种情况下a现在等于15.

In both cases a now equals 15.

这是赋值运算符,这意味着它将运算符右侧的内容应用于左侧的变量.有关赋值运算符及其功能的列表,请参见下表:

This is an assignment operator, what this means is that it applies whatever is on the right side of the operator to the variable on the left. See the following table for a list of assignment operators and their function:

Operator |  Example |  Same as    |  Result
______________________________________________
  =      |  a = 20  |             |  a = 20
  +=     |  a += 5  |  a = a + 5  |  a = 25
  -=     |  a -= 5  |  a = a - 5  |  a = 15
  *=     |  a *= 5  |  a = a * 5  |  a = 100
  /=     |  a /= 5  |  a = a / 5  |  a = 4
  %=     |  a %= 5  |  a = a % 5  |  a = 0

您还具有递增和递减运算符:

You also have the increment and decrement operators:

++--,其中++a--a分别等于21和19.您通常会发现这些用于迭代for loops.

++ and -- where ++a and --a equals 21 and 19 respectively. You will often find these used to iterate for loops.

根据顺序,您会做不同的事情.

Depending on the order you will do different things.

后缀(a++)表示法一起使用时,它首先返回数字,然后递增变量:

Used with postfix (a++) notation it returns the number first then increments the variable:

var a = 20;
console.log(a++); // 20
console.log(a); // 21

前缀(++a)一起使用时,它会递增变量,然后将其返回.

Used with prefix (++a) it increments the variable then returns it.

var a = 20;
console.log(++a); // 21
console.log(a); // 21

这篇关于javascript中的minus等于-是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆