【问题标题】:Passing the methods hide/show to another method将方法隐藏/显示传递给另一个方法
【发布时间】:2017-10-04 12:32:33
【问题描述】:

我在代码中以两种方式调用对象方法:

this.reveal.updateVisuals(i, 'show');

this.reveal.updateVisuals(i, 'hide');

我将隐藏和显示条件作为字符串传递,以供稍后评估并用作方法。请注意条件:if (effect === 'show/hide')。

 updateVisuals: function (time, effect) {
      // Check if parameter exists and property can be read
      if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
        if (effect === 'show') {
          // display the items that were fast forwarded
          var k = this.breakpointsMap[checkTime].length;
          while (k--) {
            try {
              this.breakpointsMap[checkTime][k].show();
            } catch (err) {}
          }
        } else if (effect === 'hide') {
          // display the items that were fast forwarded
          var k = this.breakpointsMap[checkTime].length;
          while (k--) {
            try {
              this.breakpointsMap[checkTime][k].hide();
            } catch (err) {}
          }
        }
      }
    }

但是代码似乎重复了,我想知道是否有一种方法可以将 hide 或 show 作为方法传递给该方法,并在需要时将其应用于数组。我尝试过这样的事情:

this.reveal.updateVisuals(i).show

【问题讨论】:

  • 你可以使用类似this.breakpointsMap[checkTime][k][effect]();

标签: javascript jquery string methods


【解决方案1】:

有很多方法可以用来简化这个,这里有几个:

updateVisuals: function (time, effect) {
  if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
      this.breakpointsMap[checkTime].forEach(e => e[effect]());
  }
}

或者返回数组:

updateVisuals: function (time, effect) {
  if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
      return this.breakpointsMap[checkTime];
  }else{
      return [];
  }
}

this.reveal.updateVisuals(i).forEach(e => e.show());

【讨论】:

    【解决方案2】:

    您可以使用 [bracket] 表示法通过它的(字符串)名称访问方法属性。

    updateVisuals: function (time, effect) {
      // Check if parameter exists and property can be read
      if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
        var k = this.breakpointsMap[checkTime].length;
        while (k--) {
          try {
            this.breakpointsMap[checkTime][k][effect]();
          } catch (err) {}
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      如果你使用的是 Es6,你可以这样做:

          function updateVisuals  (time, effect) {
          // Check if parameter exists and property can be read
              if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
                  let execFn= (arrindex) => breakpointsMap[checkTime][arrindex].show();
                  if (effect === 'hide') 
                  { 
                      execFn = (arrindex) => breakpointsMap[checkTime][arrindex].hide();
                  }     
                      // display the items that were fast forwarded
                      var k = this.breakpointsMap[checkTime].length;
                      while (k--) {
                      try {
                          execFn(k);
                      } catch (err) {}
                  }
              }
          }
      

      我假设 var checkTime 是全局的或关闭的。如果你使用的是低于 tan es6 的版本,你可以使用 execFn= function (arrindex) {...},然后在调用方法之后绑定这个参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 2016-12-21
        • 1970-01-01
        相关资源
        最近更新 更多