Skip to content

Instantly share code, notes, and snippets.

View CodeDraken's full-sized avatar

CodeDraken

View GitHub Profile
@CodeDraken
CodeDraken / katas-1.json
Created June 11, 2019 12:41
codewars katas list 1
[
{
"title": "Tribonacci Sequence",
"link": "https://www.codewars.com/kata/556deca17c58da83c00002db",
"date": "2019-06-11T12:39:57.923Z"
},
{
"title": "Playing with digits",
"link": "https://www.codewars.com/kata/5552101f47fc5178b1000050",
"date": "2019-06-12T12:39:57.923Z"
// first async example
function logMessage2 () {
console.log('Message 2')
}
console.log('Message 1')
setTimeout(logMessage2, 1000)
console.log('Message 3')
@CodeDraken
CodeDraken / blocking-code.js
Created June 12, 2019 22:23
blocking the call stack
// blocking code example
function blockingCode() {
const startTime = new Date().getSeconds()
// delay this function for 250 ms
setTimeout(function() {
const calledAt = new Date().getSeconds()
const diff = calledAt - startTime
// logs how long it took to call this function
@CodeDraken
CodeDraken / defer.js
Created June 12, 2019 22:30
setTimeout with zero delay
function defer () {
setTimeout(() => console.log('timeout with 0 delay!'), 0)
console.log('after timeout')
console.log('last log')
}
defer()
@CodeDraken
CodeDraken / memoize.js
Last active June 12, 2019 22:50
Simple memoize example using closures in javascript
// Memoization Example
// takes in a function to create a memoized version of
const memoize = (func) => {
// cache object
// keys are the arguments, values are results
const cache = {}
// returns a new function
// it remembers the cache object & func (closure)
@CodeDraken
CodeDraken / default-this.js
Last active June 19, 2019 21:24
default this example
// just logs the value of `this` to the console
function logThis() {
console.log('this =', this)
}
// DEFAULT BINDING
// undefined in strict mode
// global object in non-strict
logThis()
console.log(this) // Global/Window
@CodeDraken
CodeDraken / implicit-obects.js
Last active June 19, 2019 21:32
implicit binding on objects
function logThis() {
console.log('this =', this)
}
// named objThree just because there are more objects
// in the future examples
const objThree = {
name: 'Object Three',
logThisMethod: logThis
}
'use strict'
function logThis() {
console.log('this =', this)
}
// same example as earlier
// default binding, `this` becomes undefined
logThis()
// global in NodeJS, window in browsers
@CodeDraken
CodeDraken / explicit-binding.js
Last active June 21, 2019 03:56
explicit binding of this
//// BASE CODE ////
'use strict'
const objOne = {
name: 'Object One'
}
const objTwo = {
name: 'Object Two'
}
function logThis() {
console.log('this =', this)