1

I have some code that looks like this

var viewService = function () {
   ...
   return {
      ...
      ,isAbsolute: function (view) { ... }
      ...
      ,removeAbsoluteViews: function () { ... }
      }
   };
};

What I'd like to do is call isAbsolute from removeAbsoluteViews. When I try to do that like this

if (isAbsolute(v) === false) { ... }

I get an error saying isAbsolute is not defined. How can I do this?

Sachin Kainth
  • 43,353
  • 79
  • 196
  • 295
  • Define the scope by `this`. – Kunj Jun 06 '14 at 10:47
  • tried that - didn't work. – Sachin Kainth Jun 06 '14 at 10:48
  • if `if (this.isAbsolute(v) === false)` didn't work, it would be because `removeAbsoluteViews` didn't have the correctly scoped `this`. You should also indicate how you're calling `removeAbsoluteViews`. The following example shows how `this` scoping can fail: `setTimeout(viewService.removeAbsoluteViews, 50);` – I-Lin Kuo Jun 06 '14 at 14:43

2 Answers2

3

With a revealing module pattern

Angular Service Definition: service or factory

Community
  • 1
  • 1
Eduard Gamonal
  • 7,953
  • 5
  • 39
  • 44
3

Since you return an object you can use the this keyword:

if (this.isAbsolute(v) === false) { ... }

You can also declare it like so (without the this keyword):

var viewService = function () {

  function isAbsolute (view) { ... }

  function removeAbsoluteViews () { 
    if (isAbsolute(v) === false) { ... }
  }

  ...

  return {
    isAbsolute: isAbsolute,
    removeAbsoluteViews: removeAbsoluteViews
  };
};
Ilan Frumer
  • 31,391
  • 8
  • 68
  • 82