簡體   English   中英

將箭頭 function 轉換為常規 function - 用於 Google 跟蹤代碼管理器

[英]Converting arrow function to regular function - For use in Google Tag Manager

我想在 GTM 中使用該腳本:

 <script> var isOverIFrame = false var iframes = window.document.querySelectorAll('iframe'); function trackIframeClicks(frames){ window.addEventListener('blur',function(e) { frames.forEach(function (frame, index) { if (frame.mouseOver) { window.dataLayer.push({ 'event': 'ifameClick', 'frameSource': frame.src }); // console.log(frame.src); } }) }); } function setListeners (frames) { frames.forEach(function(frame) { frame.mouseOver = false frame.addEventListener('mouseenter', () =>{ frame.mouseOver = true // console.log('mouse in iframe') }); frame.addEventListener('mouseleave', () =>{ frame.mouseOver = false // console.log('mouse out of iframe') }); }) } setListeners(iframes); trackIframeClicks(iframes); </script>

嘗試發布時出現以下錯誤:

第 20 行和第 24 行,字符 42 中的錯誤:此語言功能僅支持 ECMASCRIPT_2015 或更好的模式:箭頭 function。

有人可以幫忙重寫 function 讓它在沒有箭頭功能的情況下工作嗎?

提前謝謝了。

    function setListeners (frames) {
  frames.forEach(function(frame) {
    frame.mouseOver = false
    frame.addEventListener('mouseenter',function enter (){
      frame.mouseOver = true
      // console.log('mouse in iframe')
    });
    frame.addEventListener('mouseleave',function leave () {
      frame.mouseOver = false
      // console.log('mouse out of iframe')
    });
  })
}
// An arrow function
() => {
  ...
}
// is shorthand syntactic sugar for
(function () { 
  ...
}).bind(this)

但是由於您不需要綁定this ,您可以放棄它並使用普通的 function。

function () {
   ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM