Node.js: how to inherit from an abstract class?

I am building a web application with node.js (+ angular, etc.).
An application will have to retrieve some data (something like a list of catalog items) from different providers who disclose their data in different ways.

In this module, I will have some function that is common to all providers, and some functions are unique to any of them.

My current (poor) implementation looks something like this:

var providers = [
  {  name: 'Apple', /* ... */ },
  {  name: 'Samsung', /* ... */ },
  {  name: 'Google', /* ... */ },
];

exports.syncCatalogues = function(search, callback) {
  var allItems = [];
  for (var p = 0; p < providers.length; p++) {
    exports.getCatalog(providers[p], function(err, catalog) {
      if (err) {
        return callback(err);
      }
      exports.getItems(providers[p], catalog, function(err, items) {
        if (err) {
          return callback(err);
        }
        allItems = allItems.concat(items);
        callback(null);
      });
    });
  }
};

And my implementation of getCatalog () and getItems () is as ugly as this:

exports.getCatalog(provider, callback) {
  if (provider.name === 'Apple') {
    // code for Apple provider ...
  }
  // and so on ...
};


exports.getItems(provider, callback) {
  if (provider.name === 'Apple') {
    // code for Apple catalog ...
  }
  // and so on ...
};

I know that with ES5 (I adhere) abstract classes are not easy to implement, but I'm sure there is a better way (the code is more readable, maintainable, verifiable) than this ... :-(

+4
source share
3

JavaScript. , , .

-, . .

var providerPrototype = {
  name: 'Prototype',
  alertName: function() {  // this is common function, all objects
    alert(this.name);      // will have it
  }
};

var appleProvider = Object.create(providerPrototype);
appleProvider.name = 'Apple';
// this is a specific function for 'Apple'
appleProvider.getCatalog = function(callback) {
  return callback(null, ['iPhone', 'Mac Mini']);
}
appleProvider.alertName = function() {
   // call 'base' method
   providerPrototype.alertName.call(this);
   alert('All rights reserved.');
}

var samsungProvider = Object.create(providerPrototype);
samsungProvider.name = 'Samsung';
// this is a specific function for 'Samsung'
samsungProvider.getCatalog = function(callback) {
  return callback(null, ['Galaxy S3', 'Galaxy S4']);
}

var providers = [
  appleProvider, samsungProvider
];

var syncCatalogues = function(search, callback) {
  var allItems = [];
  for (var p = 0; p < providers.length; p++) {
    var aProvider = providers[p];
    aProvider.getCatalog(function(err, catalog) {
      if (err) {
        return callback(err);
      }
      aProvider.alertName(); // call the base method
      alert(catalog);
    });
  }
};

syncCatalogues();
Hide result

JavaScript javascript.

node.js-.

+2

- .

javascript- mecanics. , Mootools, .

Mootools:

var Animal = new Class({
    initialize: function(age){
        this.age = age;
    }
});
var Cat = new Class({
    Extends: Animal,
    initialize: function(name, age){
        this.parent(age); // calls initalize method of Animal class
        this.name = name;
    }
});
var myCat = new Cat('Micia', 20);
alert(myCat.name); // alerts 'Micia'.
alert(myCat.age); // alerts 20.

-: http://mootools.net/

+2

I am new to nodeJS, but I think my code below is only possible after ES6. I hope to help beginners like me. In the following way:

class BaseClass {
    constructor(){
        console.log('Object created INHERITED');
    }

    toCallFromChild(){
        console.log('Called by child');
        this.toOverride();
    }

    toOverride(){} //to override
}

class childClass extends BaseClass{
    toOverride(){
        console.log ('Override by child');
    }
}

var instance = new childClass();
instance.toCallFromChild();
+1
source

Source: https://habr.com/ru/post/1628151/


All Articles