This file contains 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
// our own in memory database | |
var users = { | |
'alex': { user: 'alex', age: 27 }, | |
'john': { user: 'johndoe', age: 34 } | |
}; | |
// async function | |
var getUserFromDb = function getUserFromDb(id, callback) { | |
var min = 1000; | |
var max = 2000; |
This file contains 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
// our own in memory database | |
var users = { | |
'alex': { user: 'alex', age: 27 }, | |
'john': { user: 'johndoe', age: 34 } | |
}; | |
// async function | |
var getUserFromDb = function getUserFromDb(id, callback) { | |
var min = 1000; | |
var max = 2000; |
This file contains 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
// dividing it into chunks | |
var getSum = function getSumOfEvenNumbers(opts, cb) { | |
var sum = opts.sum || 0; | |
var i = opts.i || 0; | |
var stopAt = opts.stopAt; | |
var CHUNK_SIZE = 1000000; | |
var nextStop = (stopAt < (i + CHUNK_SIZE)) ? stopAt : (i + CHUNK_SIZE); | |
for (; i < nextStop; i++) { |
This file contains 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
var log = function(i) { | |
console.log(i); | |
} | |
function tellMeLater() { | |
for (var i = 0; i < 10; i++) { | |
setTimeout(log.bind(null, i), 100); | |
} | |
} |
This file contains 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 tellMeLater() { | |
for (var i = 0; i < 10; i++) { | |
(function(i) { | |
setTimeout(function() { | |
console.log(i); | |
}, 100); | |
}(i)); | |
} | |
} |
This file contains 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 Person(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
Person.prototype.getInfoCard = function() { | |
return this.name + ', ' + this.age + ' years old'; | |
}; | |
var johnny = new Person('John', '25'); |
This file contains 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 Person(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
Person.prototype.getInfoCard = function() { | |
return this.name + ', ' + this.age + ' years old'; | |
}; | |
// function Student(name, age, university) { |
This file contains 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
// example from http://codewinds.com/blog/2013-08-19-nodejs-writable-streams.html | |
"use strict"; | |
var stream = require('stream'); | |
var util = require('util'); | |
// use Node.js Writable, otherwise load polyfill | |
var Writable = stream.Writable; | |
var memStore = { }; |
This file contains 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
// http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html | |
"use strict"; | |
var crypto = require('crypto'); | |
var stream = require('stream'); | |
var util = require('util'); | |
var Transform = stream.Transform; | |
function ShaSum(options) { |
This file contains 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 Student(name, age, university) { | |
if (!(this instanceof Student)) { | |
return new Student(name, age, university); | |
} | |
// #1 - duplicate code from Person constructor | |
//Person.call(this, name, age); | |
this.university = university; | |
} | |
var junior2 = Student('John', 18, 'MIT'); |