This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // private properties, using closures | |
| function Ninja(){ | |
| var slices = 0; | |
| this.getSlices = function(){ | |
| return slices; | |
| }; | |
| this.slice = function(){ | |
| slices++; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // closure | |
| var a = 5; | |
| function runMe(a){ | |
| console.log(a); // 6 | |
| function innerRun(){ | |
| console.log(b); // 7 | |
| console.log(c); // undefined | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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() { |
NewerOlder