JS 006: Tag Function, default, name and Arrow Function

by Nico D

JavaScript

var version = 1;
var exampleNumber = 1;
const log = function(message) {
  if (version < exampleNumber) {
    version = exampleNumber;
    document.write("<br/>");
  };
  const title = 'EXAMPLE ' + exampleNumber + ': ';
  document.write(title + message + "<br/>");
};
//////////////////////
exampleNumber = 1;

function passthru(literals, ...substitutions) {
  let result = "";
  for (let i = 0; i < Math.max(substitutions.length, literals.length); i++) {
    if (literals[i]) {
      result += literals[i];
      log(i + ") Adding Literal = " + literals[i]);
      log(i + ") partial result: " + result);
    }
    if (substitutions[i]) {
      result += substitutions[i];
      log(i + ") Adding Substitutions = " + substitutions[i]);
      log(i + ") partial result: " + result);
    }
  }
  return result;
}
let count = 10,
  price = 0.25,
  message = passthru `${count} items cost $${(count * price).toFixed(2)}.`;
log("Result: " + message);
//////////////////////
exampleNumber = 2;
const logic = function(functionName, url, timeout, callback) {
  log(functionName + ") URL: " + url + " - TIMEOUT: " + timeout + " - CALLBACK RUN: " + callback());
};
const makeRequest = function(url, timeout, callback) {
  timeout = (typeof timeout !== "undefined") ? timeout : 2000;
  callback = (typeof callback !== "undefined") ? callback : function() {
    return "default";
  };
  //logic("makeRequest", ...arguments);
  logic("makeRequest", url, timeout, callback);
};
const makeRequest2 = function(url, timeout = 2000, callback = function() { return "default"; }) {
  logic("makeRequest2", url, timeout, callback);
};
makeRequest("/foo");
makeRequest2("/foo");
makeRequest("/foo", 500);
makeRequest2("/foo", 500);
makeRequest("/foo", 500, function() {
  return "A";
});
makeRequest2("/foo", 500, function() {
  return "A";
});
//////////////////////
exampleNumber = 3;
const params = ["/foo", 500, function() {
  return "A";
}];
const args = [];
for (let h in params) {
  args.push(params[h]);
 ...