简体   繁体   English

JavaScript-加法赋值运算符为什么不能按预期工作?

[英]JavaScript - Why doesn't the addition assignment operator work as expected?

I'm currently enhancing a website with a few animations. 我目前正在用一些动画增强网站。

I tried this code: 我尝试了这段代码:

// Opacity is 0 in the beginning. Set 
//  in CSS. 
// 1. Parameter: Boolean - true if making the
//  element visible; false if making it vanish.
// 2. Parameter: Object

var changeOpacity = function(direction, element) {
  var css = element.style;
  var goStep = function(signedStep) {
    css['opacity'] += signedStep;
    changeOpacity(direction, element);
  };

  if (direction) {
    if (css['opacity'] < 1.0) {
      setTimeout(function() {
        goStep(0.1);
      }, timeStep);
    }
  } else {
    if (css['opacity'] >= 0.1) {
      setTimeout(function() {
      goStep(-0.1);
  }, timeStep);
    } else {
      css['display'] = 'none';    
    }
  }
};

It haven't worked. 它没有用。

I wrote a few console.logs in the code: 'opacity' always stayed at 0.1 after the assignment. 我在代码中写了一些console.logs:分配后,“ opacity”始终保持在0.1。

What I expected was: 0.0 - 0.1 - 0.2 - 0.3 ... 我所期望的是:0.0-0.1-0.2-0.3 ...

Now I use this code: 现在,我使用以下代码:

// ...
var goStep = function(signedStep) {
  css['opacity'] = +css['opacity'] + signedStep;
  changeOpacity(direction, element);
};
// ...

Works fine. 工作正常。 But I still wonder why using the combined assignment addition operator failed. 但是我仍然想知道为什么使用组合的赋值加法运算符会失败。

Has anyone an idea? 有人知道吗?

You are adding string with Number, so in first case you are actually concatenating the values 您正在将字符串与Number相加,因此在第一种情况下,您实际上是在连接值

See here: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Addition_assignment 参见此处: https : //developer.mozilla.org/es/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Addition_assignment

The second aproach works because you are converting css['opacity'] to a number: +css['opacity'] 第二种方法有效,因为您正在将css['opacity']转换为数字: +css['opacity']

Try this: 尝试这个:

  var test = "0.1", test2 = "0.1"; signedStep = 0.1; test += signedStep; alert(test+" is a "+typeof test); test2 = +test2 + signedStep; alert(test2+" is a "+typeof test2); 

css['opacity'] is a string. css['opacity']是一个字符串。 If you add a number to a string, it will convert the number into a string and concat the two final strings. 如果在字符串中添加数字,它将把数字转换为字符串,并连接最后两个字符串。

css['opactity'] = 0.1
css['opacity'] += 0.5 // => "0.10.5"

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

相关问题 为什么在三元运算符的帮助下赋值在 JS 中不能按预期工作? - Why does assignment with help from ternary operator doesn't work as expected in JS? 为什么这种样式属性分配在 React Native 中没有按预期工作? - Why doesn't this style property assignment work as expected in React Native? Javascript对象添加分配运算符 - Javascript Object Addition Assignment Operator Mongodb $ 和操作员不按预期工作 - Mongodb $and operator doesn't work as expected 为什么没有 || 似乎可以作为 JavaScript 中的合并/默认运算符? - Why doesn't || seem to work as a coalesce/default operator in JavaScript? JavaScript扩展运算符和条件,为什么它不适用于数组? - JavaScript spread operator and conditional, why it doesn't work for arrays? 为什么我的 JavaScript 三元运算符不起作用? - Why doesn't my JavaScript ternary operator work? 为什么我的javascript拖动调整大小框不能按预期工作 - Why my javascript drag resize box doesn't work as expected OOP Javascript,为什么这个对象不能像预期的那样工作? - OOP Javascript, Why this object doesn't work like expected? Javascript:无法获取可变和以与加法运算符一起使用 - Javascript: Can't get variable sum to work with addition operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM