Angular JS服务原型 - 为什么不返回new()而不是使用工厂

时间:2015-10-17 13:33:34

标签: javascript angularjs prototype angularjs-service angularjs-factory

我按照Object-oriented AngularJS Services文章在我的Angular JS服务中实现了一些OOP。本文作者首先使用factory然后将其注入service - 但为什么不从[{1}}或new()返回factory对象? 像这样:

service

/** * * @param $http * @returns {CommunicationServiceImpl} * @constructor */ function CommunicationService($http){ 'use strict'; /** * * @constructor */ function CommunicationServiceImpl(){ } /** * * @param {string} path * @param {number} id * * @returns {*|Object|HttpPromise|{}|{method, params, headers}} */ CommunicationServiceImpl.prototype.getById = function(path, id) { //return suitable promise etc... }; return new CommunicationServiceImpl(); }

app.js

对我来说似乎工作得很好,在我看来这种方式更简单,除了我不了解Angular JS服务/工厂的重要内容。我是对的,还是我错了,需要按照文章的说法完成?提前感谢您的每一个答案。

0 个答案:

没有答案
相关问题