Skip to content

Instantly share code, notes, and snippets.

View mfaletti's full-sized avatar

Michael Faletti mfaletti

View GitHub Profile
// lib/runtimeroutes.js
getCustomUrls: (req) => {
let host = req.host;
let lifecycle = 'live';
if (host.includes('preview'))
lifecycle = 'preview';
else if (host.includes('staging'))
lifecycle = 'staging';
@mfaletti
mfaletti / app.js
Last active December 18, 2021 00:36
// app.js
app.get("/pg/get-custom-routes2", (req, res) => {
let output = {};
// utility function to transform database result set
let populate = function(node, obj) {
let meta = obj.metadata;
let data = meta.data;
let tmp = {}
@mfaletti
mfaletti / inheritance.js
Created February 9, 2018 07:45
The basics of how javascript prototypal inheritance works
// The basics of how prototypal inheritance works
function Person(){}
Person.prototype.dance = function(){};
function Ninja(){}
// Achieve similar, but non-inheritable results
Ninja.prototype = Person.prototype;
Ninja.prototype = { dance: Person.prototype.dance };
@mfaletti
mfaletti / object.js
Created February 9, 2018 07:20
javascript object basics
// examinining the basics of an object
function Ninja(){}
var ninja = new Ninja();
console.log( typeof ninja == "object" ? true : false) // true. the type of the instance is still an object.
console.log( ninja instanceof Ninja) // true, The object was instantiated properly.
console.log( ninja.constructor == Ninja); // true, The ninja object was created by the Ninja function.
// We can still use the constructor to build other instances.
@mfaletti
mfaletti / prototype.js
Created February 9, 2018 07:07
prototype inheritance in javascript
function gizmo(id, name) {
this.id = id;
this.name = name;
}
gizmo.prototype.toString = function(){
return this.name + this.id;
};
var hoozit = new gizmo(1, "hoozit");
@mfaletti
mfaletti / iife.js
Created February 9, 2018 07:04
Self-executing, temporary function scope
// Self-executing, temporary, function
(function(){
var count = 0;
var timer = setInterval(function(){
if ( count < 5 ) {
console.log( "Timer call: ", count ); // prints 0, 1, 2, 3, 4
count++;
} else {
console.log( count == 5 ? true : false) // true. count exists via a closure
@mfaletti
mfaletti / private.js
Created February 9, 2018 07:03
private properties, using javascript closures
// private properties, using closures
function Ninja(){
var slices = 0;
this.getSlices = function(){
return slices;
};
this.slice = function(){
slices++;
};
@mfaletti
mfaletti / closure.js
Created February 9, 2018 07:03
javascript closure example / demo
// closure
var a = 5;
function runMe(a){
console.log(a); // 6
function innerRun(){
console.log(b); // 7
console.log(c); // undefined
}
@mfaletti
mfaletti / scope.js
Created February 9, 2018 06:09
scope example in javascript
(function(){
'use strict';
// a is defined using var keyword, which means that a is local var of the IIFE.
// b is implicitly defined as a global var. code will raise an error if strict mode is on.
var a = b = 5;
})();
console.log(b); // error
(function(){
@mfaletti
mfaletti / hoisting.js
Last active February 9, 2018 06:07
hoisting example in javascript
// hoisting
// both variables and functions are hoisted (moved at the top of the function) but variables don’t retain any assigned value.
// So, at the time the variable a is printed, it exists in the function (it’s declared) but it’s still undefined
"use strict";
function test() {
console.log(a);
console.log(foo());
var a = 1;
function foo() {