Showing posts with label Study. Show all posts
Showing posts with label Study. Show all posts

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.

Thursday, February 2, 2017

Welcome

My goal with this blog will be to compliment my GitHub account where I have started to make a series of repos covering my study of the MEAN stack. MEAN is a collection of software used to make web apps and sites. The parts of the MEAN are MongoDb, Express.js, Angular.Js and Node.js.

In addition, I will record my studies in related and additional technologies as I work toward gaining mastery of Amazon Web Services (AWS) and the serverless webserver, using the services of S3, API Gateway, Lambda, DynamoDb and many more.

So, you might be curious as to, why MEAN and AWS? well, in a few words, I feel strongly that this is the future of web development.

For MEAN the reasoning is in how an application is designed as a fat client. This design sends much of the application logic to run on the client and less on the server. The MEAN stack focus on JavaScript as a unified language provides the programmer with a non-blocking code execution when using the node.js style. While very powerful it does come with a learning curve and a new set of headaches. I will try to dive into these issues at length going forward.

Amazon Web Services (AWS) offers power to the developer at a price point that is most impressive. And the one feature that really got me to notice AWS was its serverless offering. Serverless is a group of related technologies (S3, API Gateway, Lambda, DynamoDb and others)  that can serve dynamic webpages. These pages are served from AWS S3, which is a object storage service.

Objects can be HTML, CSS, JavaScript files, images and more. Since S3 supports the 3 basic files needed for a website (HTML, CSS, JS) then turning an S3 Bucket (think folder-ish) into a website must have been a natural progression for AWS. Using the API Gateway to make a REST interface will allow you perform AJAX like calls to node.js code running in a Lambda instance. Lambda is a place to store code and execute it. Lambdas have limited memory and execution time limits, but they have the ability to interact with a growing number of services designed to handle specific kinds of work.

Now all of this runs on a virtual environment that can scale dynamically as your load demands. This scaling is limited by your settings so you should not fear a run away expense.

So much to do and so little time!