我是Java的新手。
由于某些旧系统,目前我正在将一些ES6代码转换回ES5代码。
我转换了以下代码:

$row.find('.gridCellDetailAction')
 .each((i, elem) => $translate('Grid.Show' + $(elem).attr('data-title') + 's')
.then(trans => $(elem).attr('title', trans)));


对此

   $row.find('.gridCellDetailAction')
        .each(function (i, elem) {
              $translate('Grid.Show' + $(elem).attr('data-title') + 's')
   }.then(function (trans) { $(elem).attr('title', trans) }));


现在我收到以下错误:


  (中间值)。则不是函数


现在,我知道我对then做错了。但是我怎么能得到相同的结果呢?

最佳答案

通过一些格式化,您可以轻松看到差异。
一次是在then的结果上调用$translate,另一次是在函数定义上调用then

$row.find('.gridCellDetailAction')
.each(
  (i, elem) => {
    $translate(
     'Grid.Show' + $(elem).attr('data-title') + 's'
    )
    .then(
      trans => $(elem).attr('title', trans)
    )
  }
);


$row.find('.gridCellDetailAction')
.each(
  function (i, elem) {
    $translate('Grid.Show' + $(elem).attr('data-title') + 's')
  }// <-- The error is here
  .then(
    function (trans) {
      $(elem).attr('title', trans)
    }
  )
);


这是正确的:

$row.find('.gridCellDetailAction')
.each(
  function (i, elem) {
    $translate('Grid.Show' + $(elem).attr('data-title') + 's')
    .then(  //Now then is called on the result of $translate
      function (trans) {
        $(elem).attr('title', trans)
      }
    )
  }
);

关于javascript - (中间值)。则不是函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55993592/

10-10 22:00
查看更多