javascript - this.helpers 中的箭头函数

标签 javascript angularjs meteor

我想知道是否有人可以解释以下之间的区别:

$reactive(this).attach($scope);
this.helpers({
   parties: () => { //arrow function definition
     return Parties.find({});
   }
});

let reactiveContext = $reactive(this).attach($scope);    
reactiveContext.helpers({
  parties: function() { //function definition
    return Parties.find({});
  }
});

我已经阅读了有关箭头函数及其如何处理 this (即 why the `this` is not work in arrow function of ES6? )

但在这种情况下,我们如何在 helpers 中定义 party 并不重要?因为 this 在此示例中不存在。我错了吗?

我了解箭头函数在以下情况下很有用:

this.subscribe('parties', () => {
  return [
    {
      limit: parseInt(this.perPage),
      skip: parseInt((this.getReactively('page') - 1) * this.perPage),
      sort: this.getReactively('sort')
    }
  ]
});

其中,即 this (在 this.perPage 中)绑定(bind)到封闭上下文。

最佳答案

当函数内部未使用 this 时,没有区别。

它们的行为相同。

关于javascript - this.helpers 中的箭头函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34773968/

相关文章:

javascript - 在单个函数中设置状态两次 - ReactJS

javascript - 使用 jquery 或 javascript 重新加载页面时如何将文本保存在文本区域中

html - FusionChart AngularJS : Cannot display two separate charts "No Data to display"

java - 如何在java上重写url

javascript - 作为标记的指令名称不起作用

jquery - MeteorJS 基本 jquery 使用

javascript - jQuery $(form).submit() 仅针对 IE(MVC3 应用程序)不触发

javascript - 克隆原生js函数

node.js - 渲染回调中的iron-router模板数据

javascript - 使用 mongoDB 数据初始化 Bootstrap colorpicker