【问题标题】:Anybody knows what is the replacement for this code in IE?..It works in firefox,chrome.( = () => )任何人都知道 IE 中这段代码的替代品是什么?..它适用于 firefox,chrome.( = () => )
【发布时间】:2016-09-27 20:29:47
【问题描述】:

代码:

<script>
    var expectedFunc = () => showAllSteps();

    function showAllSteps() {
        alert('showAllSteps');    
        expectedFunc = () => nextStep();
    }

    function nextStep() {
        alert('nextStep');     
        expectedFunc = () => showAllSteps();
    }

    function toggleFunction() {
        expectedFunc();
    }
</script>

<button type="button" class="btn" name="showAllBtn" onclick="toggleFunction()">Show all</button>

在这段代码中,expectedFunc = () => showAllSteps();=()=> 在 IE 中不起作用。有谁知道 IE 中这个切换的替代品是什么?

【问题讨论】:

  • expectedFunc = showAllSteps; - 你只是定义一个函数,它只是调用一个函数;您也可以重新分配原始功能。

标签: javascript internet-explorer browser cross-browser


【解决方案1】:

它是ES6 arrow function,所以用简单的函数替换它

<script>
  var expectedFunc = function() {
    showAllSteps()
  };

  function showAllSteps() {
    alert('showAllSteps');
    expectedFunc = function() {
      nextStep()
    };
  }

  function nextStep() {
    alert('nextStep');
    expectedFunc = function() {
      showAllSteps()
    };
  }

  function toggleFunction() {
    expectedFunc();
  }
</script>

<button type="button" class="btn" name="showAllBtn" onclick="toggleFunction()">Show all</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2015-07-11
    • 1970-01-01
    • 2014-09-11
    • 2013-11-10
    • 2013-07-01
    • 1970-01-01
    相关资源
    最近更新 更多