惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Vercel News
Vercel News
U
Unit 42
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
N
Netflix TechBlog - Medium
GbyAI
GbyAI
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
M
MIT News - Artificial intelligence
D
Docker
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Achieving Abstraction In JavaScript
Yusuf Aytas · 2013-05-17 · via Yusuf Aytas

Published · 5 min read

In computer science, abstraction is to hide certain details and only show the essential features of the object. Abstraction tries to reduce and factor out details so that the developer can focus on a few concepts at a time. This approach improves understandability as well as maintainability of the code. While abstraction is well understood and well applied in languages like Java, C++,  this approach is not discussed much for JavaScript. In this post, we will try to give some insight for abstraction of JavaScript.

First of all, there is not built in support for traditional abstraction in JavaScript. At least, there are not any types like interfaces and abstract classes. Developers are in general does not care for abstraction. Although, ignoring abstraction does not cause any problems while writing small piece of code, it can be relatively difficult when you are dealing with a large scale application. On the other hand, abstraction provides ways of dealing with cross-cutting concerns and enables us to avoid tightly coupled code. There is a good saying about abstraction "Program to an interface, not an implementation." from Design Patterns book. To deal with complex applications one should use interfaces because using same interface you can change implementation whenever you want without much effort. For those reasons, we should have abstraction in JavaScript, too.

For abstraction, one can use interfaces and abstract classes but JavaScript does not have such features. We can alternatively define interfaces using prototypes. Let's see how we define an interface in JavaScript.

//ItemRepo interface
var ItemRepo = {
   addItem : function(item){},
   removeItem : function(id){},
   getItem:function(id){}
};

We have just defined which methods ItemRepo has. We will now define two implementations for ItemRepo : the first one is to save and retrieve data by ajax and the second one is to save and retrieve data from cookie. Let's see how we implement ItemRepo interface.

var ItemRepoAjax = function(url){
   this.url = url;
};
var ItemRepoCookie = function(){};
//Extend the ItemRepo for Ajax
ItemRepoAjax.prototype = Object.create(ItemRepo);
//Extend the ItemRepo for Cookie
ItemRepoCookie.prototype = Object.create(ItemRepo);
ItemRepoAjax.prototype.addItem = function(item){
   //actual add item code
};
ItemRepoCookie.prototype.addItem = function(item){
   //actual add item code
};

As we have defined our implementations for ItemRepo, Let's create a new class that will make use of ItemRepo.

var ItemController = function(itemRepo){
   this.itemRepo = itemRepo;
};
ItemController.prototype.add = function(item){
   itemRepo.addItem(item);
};

As long as we have two implementation for ItemRepo, we can easily switch our repository between one another to save and retrieve data. This will improve maintainability and testability of the code. We could easily mock the itemRepo object and test the rest with the mock. Moreover, we could change our way of saving and retrieving data without changing the rest of the code. Let's make use of one of the ItemController and ItemRepo.

var itemRepo = new ItemRepoAjax("url");
var itemController = new ItemController(itemRepo);
itemController.add({item:"myItem"});

As we have implemented our controller class, we may want to log or profile methods for this class. We can do that by extending ItemController and injecting a logger into it. Let's see how do that.

var Logger = {
   log : function(log){}
}
var ConsoleLogger = function() {};
ConsoleLogger.prototype = Object.create(Logger);
ConsoleLogger.prototype.log = function(log) {
   //actual logging code
}
// Define new Controller
var LoggingItemController = function(itemRepo, logger){
   this.itemRepo = itemRepo;
   this.logger = logger;
}
LoggingItemController.prototype = Object.create(ItemController);
LoggingItemController.prototype.add = function(item) {
   this.logger.log("start");
   this.itemRepo.addItem(item);
   this.logger.log("stop");
};
var consoleLogger = new ConsoleLogger();
var itemRepo = new ItemRepoCookie();
var loggingItemController = 
   new LoggingItemController(itemRepo, consoleLogger);
loggingItemController.add("item");

Moreover, we can apply classic patterns easily by using interface definitions. To make it clear, let's see how we define Factory pattern in JavaScript.

var ItemRepoFactory = {
   getItemRepo : function(type) {}
};
var ConcreteItemRepoFactory = function() {};
ConcreteItemRepoFactory.prototype = Object.create(ItemRepoFactory);
ConcreteItemRepoFactory.prototype.getItemRepo = function(type) {
   if (type === "ajax") {
      return new ItemRepoAjax("url");
   }
   return new ItemRepoCookie();
};

Using interfaces in JavaScript not only increases maintainability, it also increases testability. We can easily write mock classes to test the code. Let's see how we test the code with mock objects.

var ItemRepoMock = function(mockItem){
   this.mockItem = mockItem;
};
//Extend the ItemRepo for Ajax
ItemRepoMock.prototype = Object.create(ItemRepo);
ItemRepoMock.prototype.getItem = function(id){
   return this.mockItem;
}
function testGetItem(){
   var mockItem =  "mock";
   var mockItemRepo = new ItemRepoMock(mockItem);
   var itemController = new ItemController(mockItemRepo);
   //An equal method is needed.
   equal(itemController.getItem("id"), mockItem);
}

By defining those interfaces, one can reuse objects create by using dependency injection pattern. There are libraries that supports dependency injection like wire, but we can create a manual dependency injection on start of our application by defining every variable. Having defined dependencies at the beginning, we could easily change type of itemRepo or logger without touching any code but the configuration for dependencies.

In consequence, JavaScript is a weakly typed language and does not have classical support for abstraction. We have tried to achieve abstraction in JavaScript by defining interfaces and using them. Nevertheless, interfaces do not affect our code at all. They are nothing more than a way of documenting code for human beings. All the examples we have shown above would work exactly the same even if we completely remove the interfaces. However, it is about making roles explicit. This increases readability, understandability and maintainability of the code.