SlideShare a Scribd company logo
How Angular Embraced
Traditional OOP Design
Patterns

Ran Mizrahi (@ranm8)

Open Source Dpt. Leader @ CodeOasis
About CodeOasis

•
•
•
•

CodeOasis specializes in cutting-edge web solutions.

!

Large variety of customers (from startups to enterprises).

!

We’re passionate about JavaScript (-:

!

Technologies we love:


•
•
•
•
•
•

!

Play Framework

AngularJS 

nodeJS 

HTML5

CSS3

!

Our Microsoft department works with C#, WPF, etc.
Implementing
Design Patterns in
JavaScript
Implementing Design Patterns in JavaScript
Most traditional object-oriented languages

•
•
•
•
•

Classes.

!

Interfaces.

!

Inheritance.

!

Encapsulation.

!

Polymorphism.
Implementing Design Patterns in JavaScript
We don’t have classes…
var User = new Class({
initialize: function(name) {
this.name = name;
},
setEmail: function(mail) {
this.mail = mail;
}
});
!
var someUser = new User('Ran Mizrahi');
* MooTools Example
Implementing Design Patterns in JavaScript
We don’t have interfaces…

var DynamicMap = new Interface('DynamicMap', ['centerOnPoint', 'zoom',
'draw']);

!

function displayRoute(mapInstance) {
Interface.ensureImplements(mapInstance, DynamicMap);

!

mapInstace.centerOnPoint(12, 34);
mapInstace.draw(5);
mapInstace.zoom();
}
* Taken from the book “Pro JavaScript Design Patterns”
Implementing Design Patterns in JavaScript
We don’t have classical inheritance..
var User = new Class({
!
extends: BaseUser,
initialize: function(name) {
this.name = name;
},
setEmail: function(mail) {
this.mail = mail;
}
});
!
var someUser = new User('Ran Mizrahi');
* MooTools Example
Stop Forcing It!
Embrace It!
Learning To Embrace JavaScript

“When I see a bird that walks
like a duck and sounds like a
duck, I call that bird a duck”
— James Whitcomb Riley
Learning To Embrace JavaScript

This is how a duck looks like…

function isWindow(obj) {
return obj && obj.document && obj.location
&& obj.alert && obj.setInterval;
}
* Angular implementation of the isWindow method…
Learning To Embrace JavaScript
JavaScript is simple (can be explained over a beer) and
makes us write less code…
Java:!
  List<Book> books = new ArrayList<Book>();!

!

JavaScript:!
  var books = [];!
Learning To Embrace JavaScript
Object is a unit responsible for state and behaviour and
JavaScript function has them both.
function Unit() {
var state;
!
function behaviour() {
// Some behaviour
}

}

return function() {
// Do something
}
Learning To Embrace JavaScript
We don’t have visibility keywords (e.g. private, protected,
public) but we do have closures
var Service = (function(window, undefined) {
// Private
function base64Encode(string) {
return window.btoa(string);
}
// Public
return {
encode: function(string) {
return base64Encode(string);
}
};
}(window));
Learning To Embrace JavaScript
State management can be as easy by using constructor
functions or closures…
function someFunction(baseUrl) {
var config = {
url: baseUrl
};

};

return function() {
return config.url + '/hey';
}

function Duck(name) {
// Object state
this.name = name;
}
Learning To Embrace JavaScript
Polymorphism can be beautiful without interfaces, if it’s just a
duck…
function Duck() {
// Private state here…

!

function Bird() {
// Private state here…

!

// Public state
return {
walk: function() {
// Walk like a duck
return quack();
},

// Public state
return {
walk: function() {
// Walk like a bird
return tweet();
},

talk: function() {
// Talk like a duck
}

talk: function() {
// Talk like a bird
}

};
}

};
}
function talker(someBird) {
// Make it talk
someBird.talk();
}
Learning To Embrace JavaScript
But what will happen if the duck won’t talk…

JavaScript has prototypical inheritance but yet, I don’t often
feel I need to inherit anything (prefer polymorphic composition
over inheritance).
Learning To Embrace JavaScript
For true coverage and confidence, compilers won’t do the job
and they can’t replace real unit test coverage.

•
•
•

Almost everything is mutable.

!

Easily stub anything.

!

Easily fake dependencies.
Design Patterns in
AngularJS
MVC and Controllers
angular.module('myModule')
.controller('MyCtrl', ['$http', MyCtrl]);
!
// Dependencies are in closure (-:
function MyCtrl($http) {
var someState = {};

}

function doSomething() {
// Closure is accessible.
}

•

Controllers are just JavaScript function. 


•

They can encapsulate and preserve state by using closures.


•

!

Exposes behaviour with $scope.
Dependency Injection
angular.module('myModule')
.controller('MyCtrl', ['$http', MyCtrl]);
function MyCtrl($http) {
$http.get('http://google.com').then(getTheMonkey);
}

•

Simple dynamic dependency injection based on arrays and
naming convention. 


•

Makes your code polymorphic by easily replacing your
implementation.


•

!

Super easy to isolate and test.
Decorator
Decorator is a pattern used to “decorate” functionality of
certain object.
Decorator
Angular made service decoration really easy…
$provider.decorator('$log', ['delegate', function(delegate) {
// Convert warning to error
delegate.warn = delegate.error;
// Return the decorated service
return delegate;
}]);

Ask for a function, manipulate it and return a new one (-:
Facade
A facade is an object that provides a simplified interface to a
larger body of code and logic.
angular.module('myModule')
.factory('ReallyComplicatedService', [Service]);

!

function Service($http) {
// Do all the private stuff and handle the other library
// Expose really simple interface
return {
get: function() {
// Get it
},
del: function() {
// Delete it
}
};
}
Singleton
Singletons is a design pattern that restricts the instantiation of
a class to one object.
var Singleton = (function() {
function privateFunction() {
// Do private stuff
}
return {
someMethod: function() {
// Do public stuff
},
anotherMethod: function() {
// Do some more public stuff
}

};
}());

JavaScript makes singleton really easy (-: 

But still, it’s global and hard to configure…
Singleton
Angular used it and provided us with dependency injection to
avoid singleton downsides among others.
angular.module('myModule')
.provider('myHttp', myHttp);
function myHttp() {
var baseUrl;
this.baseUrl = function(value) {
if (!value) {
return baseUrl;
}
};

}

baseUrl = value;

this.$get = ['$q', function() {
// myHttp service implementation...
}];
Thank you!

Questions?

More Related Content

PDF
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
PDF
Intro to JavaScript Testing
Ran Mizrahi
 
PDF
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Ran Mizrahi
 
PDF
Dependency Injection @ AngularJS
Ran Mizrahi
 
PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
PDF
Javascript Best Practices
Christian Heilmann
 
PDF
JavaScript 101
ygv2000
 
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
Intro to JavaScript Testing
Ran Mizrahi
 
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Ran Mizrahi
 
Dependency Injection @ AngularJS
Ran Mizrahi
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
Javascript Best Practices
Christian Heilmann
 
JavaScript 101
ygv2000
 

What's hot (20)

PPT
Javascript and Jquery Best practices
Sultan Khan
 
PDF
Intro to JavaScript
Yakov Fain
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
ODP
Angularjs
Vincenzo Ferrari
 
PDF
Javascript Module Patterns
Nicholas Jansma
 
PDF
Java script tutorial
Doeun KOCH
 
PDF
JavaScript Modules Done Right
Mariusz Nowak
 
PDF
Dart for Java Developers
Yakov Fain
 
PDF
Workshop 12: AngularJS Parte I
Visual Engineering
 
PDF
Secrets of JavaScript Libraries
jeresig
 
PDF
JavaScript Library Overview
jeresig
 
PDF
AngularJS Basics
Ravi Mone
 
PDF
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
PDF
SproutCore and the Future of Web Apps
Mike Subelsky
 
ODP
Javascript
theacadian
 
PPTX
Angularjs Basics
Anuradha Bandara
 
PPTX
Lecture 5 javascript
Mujtaba Haider
 
PPT
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Apaichon Punopas
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
Javascript and Jquery Best practices
Sultan Khan
 
Intro to JavaScript
Yakov Fain
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Angularjs
Vincenzo Ferrari
 
Javascript Module Patterns
Nicholas Jansma
 
Java script tutorial
Doeun KOCH
 
JavaScript Modules Done Right
Mariusz Nowak
 
Dart for Java Developers
Yakov Fain
 
Workshop 12: AngularJS Parte I
Visual Engineering
 
Secrets of JavaScript Libraries
jeresig
 
JavaScript Library Overview
jeresig
 
AngularJS Basics
Ravi Mone
 
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
SproutCore and the Future of Web Apps
Mike Subelsky
 
Javascript
theacadian
 
Angularjs Basics
Anuradha Bandara
 
Lecture 5 javascript
Mujtaba Haider
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Apaichon Punopas
 
Javascript basics for automation testing
Vikas Thange
 
Ad

Viewers also liked (20)

PDF
Architektura ngrx w angular 2+
Paweł Żurowski
 
PDF
AngularJS Introduction
Carlos Morales
 
PDF
React 101
Casear Chu
 
PDF
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
PPSX
Oracle Performance Tuning Fundamentals
Carlos Sierra
 
PDF
Quick introduction to Angular 4 for AngularJS 1.5 developers
Paweł Żurowski
 
ODP
Introduction to Angular 2
Knoldus Inc.
 
PDF
Getting Started with Angular 2
FITC
 
PPTX
Drfowlerpix
Laurie Fowler
 
PPTX
Brazilian protests – June 2013
Luciana Viter
 
PDF
Wix Application Framework
Ran Mizrahi
 
PPTX
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi
 
PPT
0318
guest1a335
 
PPT
00 are you free - taxis
Luciana Viter
 
PPT
John Mitchell Presentation Oct 08 Ppt 2007
Dan Foster
 
PPT
Merkatu ikerketa-Irutxuloko Hitza
Itxaso Ferreras
 
PPT
Rochelle Michalek
jlandsman
 
PPT
Reading Revolution Blogs
Laurie Fowler
 
PDF
The Pmarca Blog Archives
Razin Mustafiz
 
PDF
"Twitter: what's all the fuss about?" / Jenni Lloyd @ Revolution Forum / Nove...
Jenni Lloyd
 
Architektura ngrx w angular 2+
Paweł Żurowski
 
AngularJS Introduction
Carlos Morales
 
React 101
Casear Chu
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
Oracle Performance Tuning Fundamentals
Carlos Sierra
 
Quick introduction to Angular 4 for AngularJS 1.5 developers
Paweł Żurowski
 
Introduction to Angular 2
Knoldus Inc.
 
Getting Started with Angular 2
FITC
 
Drfowlerpix
Laurie Fowler
 
Brazilian protests – June 2013
Luciana Viter
 
Wix Application Framework
Ran Mizrahi
 
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi
 
00 are you free - taxis
Luciana Viter
 
John Mitchell Presentation Oct 08 Ppt 2007
Dan Foster
 
Merkatu ikerketa-Irutxuloko Hitza
Itxaso Ferreras
 
Rochelle Michalek
jlandsman
 
Reading Revolution Blogs
Laurie Fowler
 
The Pmarca Blog Archives
Razin Mustafiz
 
"Twitter: what's all the fuss about?" / Jenni Lloyd @ Revolution Forum / Nove...
Jenni Lloyd
 
Ad

Similar to How AngularJS Embraced Traditional Design Patterns (20)

PPTX
All of Javascript
Togakangaroo
 
PPTX
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
FITC
 
PDF
Javascript the New Parts v2
Federico Galassi
 
PPSX
Angular js
Arun Somu Panneerselvam
 
PDF
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
PPTX
AngularJS
Yogesh L
 
PPTX
Intro to AngularJs
SolTech, Inc.
 
PDF
JavaScript Essentials
Triphon Statkov
 
PDF
The curious Life of JavaScript - Talk at SI-SE 2015
jbandi
 
PPTX
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
PDF
JavaScript Core
François Sarradin
 
PDF
Javascript Design Patterns
Subramanyan Murali
 
PPTX
Javascript Common Design Patterns
Pham Huy Tung
 
PPTX
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
PPTX
JavaScript Beyond jQuery
Bobby Bryant
 
PDF
JavaScript OOPs
Johnson Chou
 
ODP
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
PPTX
Framework prototype
DevMix
 
PPTX
Framework prototype
DevMix
 
PPTX
Framework prototype
DevMix
 
All of Javascript
Togakangaroo
 
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
FITC
 
Javascript the New Parts v2
Federico Galassi
 
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
AngularJS
Yogesh L
 
Intro to AngularJs
SolTech, Inc.
 
JavaScript Essentials
Triphon Statkov
 
The curious Life of JavaScript - Talk at SI-SE 2015
jbandi
 
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
JavaScript Core
François Sarradin
 
Javascript Design Patterns
Subramanyan Murali
 
Javascript Common Design Patterns
Pham Huy Tung
 
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
JavaScript Beyond jQuery
Bobby Bryant
 
JavaScript OOPs
Johnson Chou
 
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
Framework prototype
DevMix
 
Framework prototype
DevMix
 
Framework prototype
DevMix
 

Recently uploaded (20)

PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Software Development Company | KodekX
KodekX
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
Software Development Methodologies in 2025
KodekX
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Doc9.....................................
SofiaCollazos
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 

How AngularJS Embraced Traditional Design Patterns

  • 1. How Angular Embraced Traditional OOP Design Patterns Ran Mizrahi (@ranm8) Open Source Dpt. Leader @ CodeOasis
  • 2. About CodeOasis • • • • CodeOasis specializes in cutting-edge web solutions. ! Large variety of customers (from startups to enterprises). ! We’re passionate about JavaScript (-: ! Technologies we love: • • • • • • ! Play Framework AngularJS nodeJS HTML5 CSS3 ! Our Microsoft department works with C#, WPF, etc.
  • 4. Implementing Design Patterns in JavaScript Most traditional object-oriented languages • • • • • Classes. ! Interfaces. ! Inheritance. ! Encapsulation. ! Polymorphism.
  • 5. Implementing Design Patterns in JavaScript We don’t have classes… var User = new Class({ initialize: function(name) { this.name = name; }, setEmail: function(mail) { this.mail = mail; } }); ! var someUser = new User('Ran Mizrahi'); * MooTools Example
  • 6. Implementing Design Patterns in JavaScript We don’t have interfaces… var DynamicMap = new Interface('DynamicMap', ['centerOnPoint', 'zoom', 'draw']); ! function displayRoute(mapInstance) { Interface.ensureImplements(mapInstance, DynamicMap); ! mapInstace.centerOnPoint(12, 34); mapInstace.draw(5); mapInstace.zoom(); } * Taken from the book “Pro JavaScript Design Patterns”
  • 7. Implementing Design Patterns in JavaScript We don’t have classical inheritance.. var User = new Class({ ! extends: BaseUser, initialize: function(name) { this.name = name; }, setEmail: function(mail) { this.mail = mail; } }); ! var someUser = new User('Ran Mizrahi'); * MooTools Example
  • 10. Learning To Embrace JavaScript “When I see a bird that walks like a duck and sounds like a duck, I call that bird a duck” — James Whitcomb Riley
  • 11. Learning To Embrace JavaScript This is how a duck looks like… function isWindow(obj) { return obj && obj.document && obj.location && obj.alert && obj.setInterval; } * Angular implementation of the isWindow method…
  • 12. Learning To Embrace JavaScript JavaScript is simple (can be explained over a beer) and makes us write less code… Java:!   List<Book> books = new ArrayList<Book>();! ! JavaScript:!   var books = [];!
  • 13. Learning To Embrace JavaScript Object is a unit responsible for state and behaviour and JavaScript function has them both. function Unit() { var state; ! function behaviour() { // Some behaviour } } return function() { // Do something }
  • 14. Learning To Embrace JavaScript We don’t have visibility keywords (e.g. private, protected, public) but we do have closures var Service = (function(window, undefined) { // Private function base64Encode(string) { return window.btoa(string); } // Public return { encode: function(string) { return base64Encode(string); } }; }(window));
  • 15. Learning To Embrace JavaScript State management can be as easy by using constructor functions or closures… function someFunction(baseUrl) { var config = { url: baseUrl }; }; return function() { return config.url + '/hey'; } function Duck(name) { // Object state this.name = name; }
  • 16. Learning To Embrace JavaScript Polymorphism can be beautiful without interfaces, if it’s just a duck… function Duck() { // Private state here… ! function Bird() { // Private state here… ! // Public state return { walk: function() { // Walk like a duck return quack(); }, // Public state return { walk: function() { // Walk like a bird return tweet(); }, talk: function() { // Talk like a duck } talk: function() { // Talk like a bird } }; } }; } function talker(someBird) { // Make it talk someBird.talk(); }
  • 17. Learning To Embrace JavaScript But what will happen if the duck won’t talk… JavaScript has prototypical inheritance but yet, I don’t often feel I need to inherit anything (prefer polymorphic composition over inheritance).
  • 18. Learning To Embrace JavaScript For true coverage and confidence, compilers won’t do the job and they can’t replace real unit test coverage. • • • Almost everything is mutable. ! Easily stub anything. ! Easily fake dependencies.
  • 20. MVC and Controllers angular.module('myModule') .controller('MyCtrl', ['$http', MyCtrl]); ! // Dependencies are in closure (-: function MyCtrl($http) { var someState = {}; } function doSomething() { // Closure is accessible. } • Controllers are just JavaScript function. 
 • They can encapsulate and preserve state by using closures. • ! Exposes behaviour with $scope.
  • 21. Dependency Injection angular.module('myModule') .controller('MyCtrl', ['$http', MyCtrl]); function MyCtrl($http) { $http.get('http://google.com').then(getTheMonkey); } • Simple dynamic dependency injection based on arrays and naming convention. 
 • Makes your code polymorphic by easily replacing your implementation. • ! Super easy to isolate and test.
  • 22. Decorator Decorator is a pattern used to “decorate” functionality of certain object.
  • 23. Decorator Angular made service decoration really easy… $provider.decorator('$log', ['delegate', function(delegate) { // Convert warning to error delegate.warn = delegate.error; // Return the decorated service return delegate; }]); Ask for a function, manipulate it and return a new one (-:
  • 24. Facade A facade is an object that provides a simplified interface to a larger body of code and logic. angular.module('myModule') .factory('ReallyComplicatedService', [Service]); ! function Service($http) { // Do all the private stuff and handle the other library // Expose really simple interface return { get: function() { // Get it }, del: function() { // Delete it } }; }
  • 25. Singleton Singletons is a design pattern that restricts the instantiation of a class to one object. var Singleton = (function() { function privateFunction() { // Do private stuff } return { someMethod: function() { // Do public stuff }, anotherMethod: function() { // Do some more public stuff } }; }()); JavaScript makes singleton really easy (-: 
 But still, it’s global and hard to configure…
  • 26. Singleton Angular used it and provided us with dependency injection to avoid singleton downsides among others. angular.module('myModule') .provider('myHttp', myHttp); function myHttp() { var baseUrl; this.baseUrl = function(value) { if (!value) { return baseUrl; } }; } baseUrl = value; this.$get = ['$q', function() { // myHttp service implementation... }];