Skip to content

Instantly share code, notes, and snippets.

JavaScript Fundamentals

Part 1: Variables

Var & Scope

var greeting = 'Hello';
console.log(greeting); // prints: Hello

if (true) {
  var greeting = 'Hi';
@polotek
polotek / design_docke_template.md
Created April 3, 2019 03:06
Design Doc Template

Design Doc Title

Stakeholders

List stakeholders for project/feature.

Role Person
Dev Owner
Other Dev 1 (Engine)
JSON._dateReviver = function (k,v) {
if (v.length !== 24 || typeof v !== 'string') return v
try {return new Date(v)}
catch(e) {return v}
}
JSON.parseWithDates = function (obj) {
return JSON.parse(obj, JSON._dateReviver);
}
@polotek
polotek / db-launch.sh
Last active April 20, 2016 05:47 — forked from fancyremarker/db-launch.sh
Example: db-launch script to launch the Aptible postgresql database image
# A script for creating aptible postgres databases in containers
container=$(head -c 32 /dev/urandom | md5);
username=${USERNAME:-aptible};
passphrase=${PASSPHRASE:-password};
dbname=${DATABASE:-db}
cname=${CNAME}
extport=${EXTPORT:-5432}
image="${@: -1}";
# Create a data volume
var request = require('request')
, gunzip = require('zlib').createGunzip()
, json = new (require('jstream'))();
request('http://data.githubarchive.org/2012-03-11-12.json.gz')
.pipe(gunzip)
.pipe(json)
.on('data', function(obj) {
console.log(obj);
});
@polotek
polotek / device.js
Created May 11, 2012 05:27 — forked from rwaldron/device.js
Fat Arrows and Classes-as-Prototype-sugar make a beautiful future for JavaScript
var stream = require("fake-stream-lib"),
Emitter = require("events").EventEmitter,
util = require("util");
// Today...
function Device( opts ) {
this.value = null;
@events =
events: {}
on: (topic, handler, context = this) ->
(@events[topic] or= []).push {handler, context}
trigger: (topic, args...) ->
return unless @events[topic]?
handler.apply(context, args) for {handler, context} in @events[topic]
@polotek
polotek / benchCallbacks.js
Created April 12, 2012 00:24 — forked from bjouhier/benchCallbacks.js
streamline vs. callbacks bench
"use strict";
var fs = require('fs');
var cache = {}, hit = 0, missed = 0;
function load(name, cb) {
var res = cache[name];
if (res) {
process.nextTick(function() {
hit++;
cb(null, res);
@polotek
polotek / route.js
Created December 6, 2011 18:50 — forked from creationix/route.js
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
render("frontindex", {
title: query("index", "title"),
links: query("index", "links"),
articles: loadArticles
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"
@polotek
polotek / route.js
Created December 5, 2011 19:19 — forked from creationix/route.js
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
loadIndex(function (err, home) {
home.render("frontindex",
links: query("index", "links"),
articles: loadArticles()
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"