toddmotto / angularjs-styleguide

AngularJS styleguide for teams
https://ultimateangular.com
5.96k stars 700 forks source link

host Object instead of revealing module pattern in Factories #32

Closed nick-brown closed 8 years ago

nick-brown commented 9 years ago

Always return a host Object instead of the revealing Module pattern due to the way Object references are bound and updated

This is confusing. How is this not the revealing module pattern? Also, aren't host objects something like document, window, etc?

I also don't fully understand what problem this is trying to solve.

haruair commented 9 years ago

I have a same question about the section. I'm not sure, but I can explain how I understand the guide. The code is the difference between returning a host object and the revealing module pattern below:

// the normal style of revealing module pattern
function SomeService () {
  var someCreepyValue  = 'spongebob';
  function someMethod () {
    return someCreepyValue;
  };

  return {
    someMethod: someMethod
  };
}

// confusing style of revealing module pattern
function OtherService () {
  var OtherService = {};
  var otherCreepyValue = 'spongebob';
  OtherService.someMethod = function () {
    return otherCreepyValue;
  };
  return OtherService;
}

// return all as a host Object (this is the guide one)
function AnotherService () {
  var AnotherService = {};
  AnotherService.openValue = 'spongebob';
  AnotherService.someMethod = function () {
    return AnotherService.openValue;
  };
  return AnotherService;
}

The point of the Revealing Module Pattern is that we can control the accessibility of the methods like public/private. As you can see Why?, The pattern doesn't allow to change the value if the service used the primitive value (someCreepyValue, otherCreepyValue on above.)

Returning a host object is better than the pattern, because of the complexity, even we can create get/set method for the private value.

toddmotto commented 8 years ago

Closing off, the styleguide has just been completely rewritten in ES6 for Angular 1.5+ (component architecture/components/one-way dataflow etc).