angularjs - Why functions are returned from angular services -


while defining angular services, 1 expected pass constructor function. constructor function initialized using "new" keyword , object returned "new" set service. far good.

however come across instances programmers creating services below:

app.service('aobj', function aclass(){    return function(a,b){     alert(a + b);   };  }); 

this called

aobj(1,2) 

although undertsand aobj end being function, cannot understand why programmers initialize services way. constructor functions have definition like:

app.service('aobj', function aclass(){    this.var1 = undefined;   this.calculate = function(b){     alert(this.var1 + b);   };  }); 

then 1 call aobj.var1 = 1; aobj.calculate(2);

can elaborate on purpose of defining services using former method ?

plnkr: http://plnkr.co/edit/tpl:frtqqtnoy8befhs9bb0f 

this depends on kind of api want expose service.

return object , call them myservice.methoda() , myservice.methodb() if service container exposing several closely related methods. angular's $location service example of this.

if service exposes single public method, can more convenient users of service call myservice() rather myservice.theonlymethod(). angular's $compile service example of this.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -