Saturday, March 4, 2017

JS Higher-order Functions and Class Inheritance

I was planning on making a tutorial to explore JS higher-order functions, but during my research I found an interesting article that gives a very good introduction of the different types...

https://www.sitepoint.com/demystifying-javascript-closures-callbacks-iifes/

Be certain you read the comments as it has a nice debate on the closure examples that could offer more insight into them.

I had a question on the IFFY 4th function example which produced a undefined result. After playing in JS Bin, I was able to get a result outside of the IFFY with this...

var test = function(){

  var today = new Date();
  var currentTime = today.toLocaleTimeString();
  // output: the current local time (e.g. 7:08:52 PM)
  console.log(currentTime);   
  return { currentTime:currentTime };
}();

console.log(test.currentTime);   // output: current time

Segway


The idea of the return value came to me from a project I did for work. In that I converted a set of PHP classes to Node.js. while working on it I needed to find a way to implement classes with inheritance in Node.js. So I found an article on it a while back and did not write down the source (grr). So below is the basic idea on the structure:

base_class.js:
"use strict";
// private var
var name = '';

// public
function base(){
  console.log("base");
  function set_name(input){
    name = input;
  }
  function get_name(){
    return name;
  }
  
  return { 
    set_name:set_name,
    get_name:get_name
  };
}
module.exports = base;

The base class will define all of the common functions used in the inherited classes. You could also include additional features, like a debug flag, connection data, etc...

inherited_class.js:
"use strict";
var base_class = require("./base_class.js");

var inherit = {};
inherit.__proto__ = base_class();

function inherited(input){
  console.log("inherited");

  var inherited = {};
  inherited.myname = function(input){
    inherit.set_name(input);
    return inherit.get_name();
  }
  
  return inherited;
}
module.exports = inherited;


Calling the inherited class in the final class permits you to use the public functions and variables declared from it. While the base class is called from outside of the function "inherited" and hides its public functions and variables.

final_class.js:
"use strict";
var inherited = require("./inherited_class.js");
var naming = new inherited();


console.log("My name is: "+naming.myname("Chris"));

// calling this will cause an error, so no access to base class
// console.log("Calling Base Class Function: "+naming.get_name());

Calling these "classes" is most likely wrong in the classic sense, but I was more interested in providing a modular separation of code and inherit common functions.

The real code I made was connecting to a 3rd party API. This API had different parts like send customer data as one API and lookup customer as a separate API. Since both the send and lookup APIs used the same basic connection data and common function. Building my code like this made sense...at least to me.

Is this the best way to design code for this use case? I am not certain! From my point of view making code that works is more the point of my job than conforming to a purist point of view. On the other hand, conforming to a more purist coding practice makes understanding each others code easier.

Perhaps it is better to just admit, I have more to explore.

No comments:

Post a Comment