Skip to content

Instantly share code, notes, and snippets.

View aquaductape's full-sized avatar
🐢
🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢

Caleb Taylor aquaductape

🐢
🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢
View GitHub Profile
@aquaductape
aquaductape / myForEach.js
Last active June 27, 2019 07:25
emulating forEach method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'myForEach', {
// if the callback is an arrow function then context will be set to outer scope regardless of providing context argument
// On the other hand if callback is a regular function expression, if context is not provided then the 'this'
// will be set to window(or undefined if strict) since the where the callback function is invoked, it is not an object property,
// but rather just a function pattern --> https://stackoverflow.com/a/2752318/8234457
// tldr; The 'this' keyword’s value has nothing to do with the function itself,
// it's how the function is called determines 'this's value
@aquaductape
aquaductape / myFilter.js
Last active June 27, 2019 16:31
emulating filter method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'myFilter', {
// if callback is arrow function then context will be set to outer scope regardless of providing context argument
value: function(callback, context) {
// Good example for using the context with regular function expression
// const as = new Set([1, 2, 3]);
// const bs = new Set([3, 2, 4]);
// const intersection = [...as].filter(bs.has, bs); // [2, 3]
if (typeof callback !== 'function') {
@aquaductape
aquaductape / myMap.js
Last active July 4, 2019 08:58
emulating map method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'myMap', {
// if callback is arrow function then context will be set to outer scope regardless of providing context argument
value: function(callback, context) {
'use strict';
// will context to undefined if it is global object
if (typeof callback !== 'function') {
throw new TypeError(`${callback} is not a function`);
@aquaductape
aquaductape / myFind.js
Last active June 19, 2019 20:54
emulating find method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'myFind', {
// if callback is arrow function then context will be set to outer scope regardless of providing context argument
value: function(callback, context) {
if (typeof callback !== 'function') {
throw new TypeError(`${callback} is not a function`);
}
const length = this.length;
@aquaductape
aquaductape / mySome.js
Last active June 19, 2019 21:00
emulating some method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'mySome', {
// if callback is arrow function then context will be set to outer scope regardless of providing context argument
value: function(callback, context) {
if (typeof callback !== 'function') {
throw new TypeError(`${callback} is not a function`);
}
const length = this.length;
@aquaductape
aquaductape / myEvery.js
Last active June 19, 2019 20:58
emulation of every method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'myEvery', {
// if callback is arrow function then context will be set to outer scope regardless of providing context argument
value: function(callback, context) {
if (typeof callback !== 'function') {
throw new TypeError(`${callback} is not a function`);
}
const length = this.length;
@aquaductape
aquaductape / myReduce.js
Last active June 19, 2019 20:43
emulating reduce method
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'myReduce', {
value: function(callback, initialValue) {
if (typeof callback !== 'function') {
throw new TypeError(`${callback} is not a function`);
}
// throw error if array is empty, even a sparse array
// use filter(or use myFilter) method to remove empty values
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
// Array prototype method, however this is not safe, since it is added to the global scope
// definedProperty makes method non-enumerable as well as adding new property to in this case, to Array prototype
Object.defineProperty(Array.prototype, 'mySort', {
// webkit and chrome uses quicksort
// mozilla uses merge sort
// For my sort algorithm, it uses merge sort
// But I also chose chrome's interpretation of callback return boolean value, just because I mainly use chrome
value: function(callback) {
@aquaductape
aquaductape / mergeBottomUp.js
Last active June 20, 2019 16:28
most merge sort solutions are top down, which copy the array. I need an approach to mutates the array which is merge bottom up
const mergeSortBottomUp = arr => {
const length = arr.length;
let step = 1;
while (step < length) {
let left = 0;
while (left + step < length) {
mergeBottomUp(arr, left, step);
left += step * 2;
}
step *= 2;
@aquaductape
aquaductape / understanding_this.md
Last active July 2, 2019 16:54
understanding THIS in JavaScript

Understanding THIS

my guide is just a reinforcement by different sayings, bunch of quick tips and links on how to eyeball the determination what 'this'.

Determining THIS in Regular Function Expressions

function () { console.log(this) }

1.

  if called in the form of obj.func()
                       "this" === obj
                                 else