代码之家  ›  专栏  ›  技术社区  ›  Pete Irfan TahirKheli

DataTables、es6 arrow函数和

  •  2
  • Pete Irfan TahirKheli  · 技术社区  · 7 年前

    datatables 我使用 preDrawCallback

    由于它是一个webpack项目的一部分,我已经将其转换为es6,但我不得不禁用该函数的对象速记,因为我无法找到不使用 this :

    $tables.DataTable({
      preDrawCallback: function(settings) { // eslint-disable-line object-shorthand
    
        const $table = $(this); // is there a way to get this table without using this?
        
        // rest of code
      }
    })

    $(this)

    我的问题是,我是否必须使用这个函数,或者是否有其他方法获取表,以便将命名函数更改为箭头函数?

    更新

    似乎我看错了问题,根本不需要使用箭头函数,而修复lint错误解决了不需要使用箭头函数的问题 ,在控制台记录了很多不同的内容之后,我发现当前表id保存在传入的设置中,以便您可以执行以下操作

    $(`#${settings.sTableId}`).DataTable();
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Nicholas Tower    7 年前

    如您所说,如果值没有作为参数传入,则需要使用普通函数。正规函数和箭头函数对于 this ,所以你需要为工作选择合适的工具。

    至于您看到的lint错误,这与arrow函数无关。它只是告诉你这样写:

    $tables.DataTable({
      preDrawCallback(settings) { // <-- using shorthand on this line
        const $table = $(this); 
    
        // rest of code
      }
    });