Skip to content

Instantly share code, notes, and snippets.

@0bie
0bie / deepCopy.js
Last active September 21, 2022 17:19
/**
* check if vals are objects
* if so. copy that object (deep copy)
* else return the value
*/
function deepCopy(obj) {
const keys = Object.keys(obj);
const newObject = {};
for (let i = 0; i < keys.length; i++) {
@0bie
0bie / sqlNotes.md
Created December 29, 2017 23:16
Learning SQL

SQL

Log into database: mysql [db_name]

View a table: SELECT * FROM table_name;

Update a record in a table:

UPDATE [table_name] SET [table_column] = NOW() WHERE id = 1;

@0bie
0bie / boolean.js
Last active November 8, 2017 19:21
Convert values to boolean in JS
// In JS `!!` can be used to convert any value to a boolean
// Similar to using the Boolean() function
(function() {
var foo = 'foo';
console.log(typeof foo);
var bool = !!foo;
console.log(typeof bool);
console.log(bool);
@0bie
0bie / objectCreation.js
Last active October 20, 2017 09:30
Creating objects in JavaScript
// `bind` and `this`
let dog = {
sound: 'woof',
talk: function() {
console.log(this.sound);
}
}
dog.talk(); // => 'woof'
@0bie
0bie / modules.js
Last active September 27, 2017 05:26
JS Module Patterns
// Anonymous function:
// Ex.1
(function() {
// We keep these variables private inside this closure scope
var myGrades = [93, 95, 88, 0, 55, 91];
var average = function() {
var total = myGrades.reduce(function(accumulator, item) {
return accumulator + item;
}, 0);
return 'Your average grade is ' + total / myGrades.length + '.';
@0bie
0bie / value-reference.js
Created September 12, 2017 23:23
Understanding JS value and reference
// JS has 5 data types that are assigned by value:
// `Boolean`, `String`, `Number`, `null`, `undefined` (primitives)
// JS has 3 data types that are assigned by reference:
// `Array`, `Function`, `Object` (objects)
var x = 1, y = '1', z = undefined;
// When we assign these vars to other vars we copy the value to the new var
var a = x, b = y, c = z;
@0bie
0bie / observerPattern.js
Created August 25, 2017 17:47
Understanding the observer pattern
// Observer Pattern Notes
// The instance (subject) maintans a collection of objects (observers)
// The instance notifies the objects when changes to the state occur
class Observable {
// each instance starts with an empty array of observers that react to state changes
constructor() {
this.observers = [];
}
// Lifecycle phases:
// Initiialization: Defaults & initial values for `this.props` and `this.state`
// `getDefaultProps()` is called once and cached when the class is created
// We can't rely on `this.props` in `getDefaultProps()`
// `getInitialState()` is invoked once right before the mounting phase
var Counter = React.createClass({
getDefaultProps: function() {
console.log('getDefaultProps');
@0bie
0bie / eventLoop.js
Created June 26, 2017 02:41
Understanding the event loop
// Passing a value of 0 to the `setTimeout` method deffers the callback to the end of the stack or until the stack is clear
setTimeout(function() {
console.log('test');
}, 0);
// `setTimoeout` is not a guaranteed time to exucution, it's a minimum time to execution
// Passing a value of 0 to `setTimeout` doesn't run the code immediately, it runs the code as soon as the stack is clear (nextish)
// The event loop pushes any deffered executions in the task/callback que only after the stack is clear
// The stack can continue to run while waiting for a request from the web API
// The browser can't perform a render while code is running on the stack
// Avoid putting slow/heavy duty code on the stack because it prevents browsers from rendering the UI
@0bie
0bie / sessions.js
Last active January 13, 2021 00:04
Understanding sessions in Express
// Sessions
// A place to store data that you want access to across requests.
// Each user that visits your site has a unique session
// Sessions can be used to store and access user data as they access your application
// Sessions allow a web app to store state
// Simple session in Express:
import express from 'express';
import session from 'express-session';