AngularJS设计模式:我应该使用工厂创建构造函数? [英] AngularJS design patterns: Should I use factories to create constructor functions?

查看:1018
本文介绍了AngularJS设计模式:我应该使用工厂创建构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在思索,同时创造一个AngularJS应用。当我第一次了解了AngularJS工厂,我想其中的一个巧妙的用法是创建并返回一个构造函数,而不是一个普通的对象,即是这样的:

This is something I've been mulling over while creating an AngularJS app. When I first learned about AngularJS factories, I thought one clever usage of them would be to create and return a constructor function rather than a plain object, i.e. something like:

app.factory("Foo", function() {
  function Foo(bar, baz) {
    this.bar = bar;
    this.baz = baz;
    ...
  }

  Foo.prototype = {
    constructor: Foo,
    method1: function() { ... },
    method2: function() { ... },
    ...,
    methodn: function() { ... },
  };

  return Foo;
});

然后,您可以注入的功能到您的控制器,并与叫它新。我发现这个美观和OOP-Y,但我现在开始觉得它实际上是一个反模式。问题是,它工作得很好,当你在AngularJS感知上下文工作,但一旦你想,例如,呼叫从控制台的构造,在Web工作者使用,或重复使用code在非AngularJS应用程序,你开始有工作的围绕的AngularJS,而不是用它。我开始怀疑,如果这个方法被误导只要在JavaScript函数似乎已经是单身,似乎没有需要被实例化的任何帮助。

Then, you could inject the function into your controllers and call it with new. I found this aesthetically pleasing and OOP-y, but now I'm starting to think that it's actually an anti-pattern. The problem is that it works fine for when you're working within AngularJS-aware contexts, but once you want to, for instance, call the constructor from the console, use it in a Web Worker, or reuse the code in a non-AngularJS app, you start having to work around AngularJS rather than with it. I began to wonder if this approach was misguided insofar as functions in javascript already seem to be "singletons" and don't seem to need any help being instantiated.

我是不是滥用AngularJS工厂?我会用暴露在全球范围内构造函数得到更好的服务?更一般地,在那里它促进AngularJS的使用对全球工厂的对象/服务/供应商或反之亦然?

Am I misusing AngularJS factories? Would I be better served with constructor functions exposed to the global scope? More generally, are there specific factors which promote the usage of AngularJS factories/services/providers over global objects or vice versa?

推荐答案

是的!

工厂语法:module.factory('factoryName',功能);结果:
  当声明factoryName的注射参数,你会
  只要是通过调用函数参照返回的值
  传递给module.factory。 使用方法:可能是返回一个有用的
  然后可以new'ed创建实例类的功能。

Factories Syntax: module.factory( 'factoryName', function ); Result: When declaring factoryName as an injectable argument you will be provided the value that is returned by invoking the function reference passed to module.factory. Usage: Could be useful for returning a 'class' function that can then be new'ed to create instances.

来源: https://groups.google.com/forum/ #!MSG /角/ 56sdORWEoqg / HuZsOsMvKv4J

上面的链路也被用作源到<一href=\"http://stackoverflow.com/questions/18072683/angularjs-design-patterns-should-i-use-factories-to-create-constructor-function#comment26450279_18072683\">Bart's评论:<一href=\"http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory\">Angular.js:服务提供商VS VS工厂?

The above link is also used as the source to Bart's comment: Angular.js: service vs provider vs factory?

这篇关于AngularJS设计模式:我应该使用工厂创建构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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