使用ES6类语法在控制器中分配依赖关系 [英] Assigning dependencies in controllers using the ES6 class syntax

查看:160
本文介绍了使用ES6类语法在控制器中分配依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2015年ng-conf的视频( Angular 1.3会议Angular 2.0 )中,语法使用ES6类作为控制器显示为:

  class UnicornHype {
constructor(unicornHorn,$ q){
这个$ q = $ q;
this.horn = unicornHorn
}

findUnicorn {
return this。$ q((resolve,reject)=> {
...
this.horn.thrust();
...
});
}
}

我看到注入的依赖项被分配为实例属性我想知道这是否是一个很好的方法。由于控制器的依赖关系通常是单例服务,所以不应该被这些实例共享?



他们这样做的原因是,以前使用的方法 $ scope (因此在构造函数的正文中)现在在对象的共享原型上。 John Papa的风格指南实际上将它们直接分配给这个(虽然他没有使用ES6类 - 但是这并不重要,因为它们只是构造函数的原型中的语法糖)。这是一个好主意吗?



另一种方法是在原型上保留方法,但将依赖关系分配给局部变量(假设每个控制器都在自己的模块文件中)。如下所示:

  var q,horn; 
class UnicornHype {
构造函数(unicornHorn,$ q){
[q,horn] = [$ q,unicornHorn];
}
findUnicorn {
return q(...);
}
}

这更好吗?如果是,那么 const 实际上要比 var 更好吗?这个方法有什么缺点吗?



这里描述了第三种方法(使用 WeakMaps ):使用ES6编写AngularJS应用程序。我应该忘记上面所说的一切,这样做吗?

解决方案

我不太明白为什么他们使用Weakmaps。
我引用:


选择WeakMap之后的原因是,将对象作为键的WeakMap的条目一旦被删除,垃圾收集。


但不是服务长寿吗?那么为什么你需要确保垃圾回收?



在javascript中,所有非原语都是指向原始实例的指针,因此依赖关系始终是共享的。那么为什么实例变量方法不是一个好主意?



无论如何,我认为实例变量方法似乎是最有前途的方法。 p>

In a video from ng-conf 2015 (Angular 1.3 meets Angular 2.0), the syntax for using ES6 classes as controllers is shown as:

class UnicornHype {
  constructor(unicornHorn, $q) {
    this.$q = $q;
    this.horn = unicornHorn
  }

  findUnicorn {
    return this.$q((resolve, reject) => {
      ...
      this.horn.thrust();
      ...
    });
  }
}

I see that the injected dependencies are assigned as instance properties and I'm wondering if that's a good way to do that. Since the controller's dependencies are usually singleton services, shouldn't they be shared by the instances?

The reason they've done it like this is that methods that were previously on $scope (and therefore in the constructor function's body) are now on the object's shared prototype. John Papa's style guide actually assigns them directly to this (though he's not using ES6 classes - but that shouldn't really matter since they're just syntactic sugar on the constructor function prototype stuff). Is that a good idea?

Another approach would be to keep methods on the prototype but assign the dependencies to local variables (assuming each controller is in its own module file). Something like:

var q, horn;
class UnicornHype {
  constructor(unicornHorn, $q) {
    [q, horn] = [$q, unicornHorn];
  }
  findUnicorn {
    return q(...);
  }
}

Is this better? If yes, would const actually be better than var here? Does this approach have any drawbacks?

A third method (using WeakMaps) is described here: Writing AngularJS Apps Using ES6. Should I forget everything I said above and do it this way?

解决方案

I don't really understand why they use Weakmaps. I quote:

Reason behind choosing WeakMap is, the entries of WeakMap that have objects as keys are removed once the object is garbage collected.

But aren't services long-lived? So why would you need to ensure garbage collection?

In javascript all non-primitives are pointers to the original instance, so the dependencies are always shared. So why would the instance-variable approach not be a good idea?

Anyway, I think the instance-variable approach seems the most future-proof way to go.

这篇关于使用ES6类语法在控制器中分配依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆