Skip to content

Instantly share code, notes, and snippets.

@0bie
0bie / promise.js
Last active September 21, 2022 17:32
Learning ES6 Promise
// callback examples
function loadImage(src, parent, callback) {
const img = document.createElement('img');
img.src = src;
img.onload = callback;
parent.appendChild(img);
}
const imgContainer = document.body;

Object property descriptors

Provide a way to draw a distinction between the characteristics of properties, such as whether the property was read-only or not.

var myObject = {
  a: 2
};

Object.getOwnPropertyDescriptor(myObject, 'a'); // => {value: 2, writable: true, enumerable: true, configurable: true}
@0bie
0bie / jsTerms.md
Last active October 18, 2016 16:13
JS terms
  • Call-site: The location in code where a function is called (not where it's declared). Usually the call-site we care about is in the invocation before the currently executing function

  • Call-stack: The stack of functions that have been called to get us to the current moment in execution

  • Closure:

    • When a function still has reference to its outer scope (parameters, variables, functions) even after that outer function has been executed.

    • Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope E.g

@0bie
0bie / scopeResolution.md
Created October 6, 2016 21:44
Scope Resolution in JS
Scope Resolution
  • The ability to store values and pull values out of variables is what gives a program state.

  • A well defined set of rules for storing variables in some location, and for hiding those variables at a later time: SCOPE

  • Abstract Syntax Tree (AST): The grammatical structure of a program

  • Any snippet of JS has to be compiled before (usually right before) it's executed

@0bie
0bie / functions.md
Last active September 21, 2022 17:50
JS functions
Functions
  • When a function is invoked on or through an object, that object is the invocation context or this value for the function.

  • Functions designed to initialize a newly created object are called constructors

@0bie
0bie / npm.md
Last active September 21, 2022 17:54
npm beginner notes

npm commands

  • Update npm: npm install npm --global || npm i npm -g
  • New package: npm init --yes || npm init -y
  • Scopes: npm init --scope=myusername npm install @myusername/mypackage require('@myusername/mypackage')
  • Add dependencies: npm install --save package-name || npm i -s package-name
  • Add devDependencies: npm install --save-dev package-name || npm i -D package-name
  • Skip devDependencies: npm install --production
  • Add bundled dependencies: npm install --save --save-bundle package-name
  • Update dependencies: npm outdated && npm update
@0bie
0bie / functionalSnippets.js
Last active September 21, 2022 10:56
functional js snippets
const overlay = (element) => {
const newElement = document.createElement(element);
newElement.style.height = '100vh';
return newElement;
};
const onEvent = (inputType, DOMElement, callback) => {
if (DOMElement.addEventListener){
DOMElement.addEventListener(inputType, callback);
}
@0bie
0bie / decorators.md
Last active September 21, 2022 18:00
Working with decorators in React
  • An ES2016 decorator is an expression which returns a function and can take a target, name and property descriptor as arguments.
  • A decorator takes an argument (the function being decorated) and returns the same function with added functionality.
  • Decorators are helpful for anything you want to transparently wrap with extra functionality.
  • Its simplest form (React):
const HOC = (Component) => (props) => <Component {...props} />;