Skip to content

Instantly share code, notes, and snippets.

Chase Isley theonlychase

Block or report user

Report or block theonlychase

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View regex
// Add a variable to a Regular expression
function replace(str, r) {
let regex = new RegExp(`[${r}]`, "g");
return str.replace(regex, "#")
}
replace("abcdef", "c-e")
View generators
## Generators
A generator is a function that can stop midway and then continue from where it stopped.
In short, a generator appears to be a function but it behaves like an iterator.
In JavaScript, a generator is a function which returns an object on which you can call `next()`
```javascript
{
View promise
## Promises
What is a promise?
1. An object that may produce a single value in the future
1. Either a resolved value
2. Or a reason why it was not resolved
2. Three states
1. Pending
View wrapper-functions
function wrap (execute) {
try {
var result = execute.apply(this, arguments);
if (result == "Result") return (function() { return "Result"; });
} catch (e) {
if (e instanceof Error) return (function() { return null; });
}
}
var errorExec = wrap(function () {
@theonlychase
theonlychase / sets
Last active Apr 28, 2019
Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection. It is different than an array in that you can't access the items
View sets
## Sets
Set objects are collections of unique values. You can iterate through the elements of a set in insertion order.
A value in the Set may only occur once; it is unique in the Set's collection.
It is different than an array in that you can't access the items individually and it is not index based
Think of it as a list of items which we can add to, remove from, and loop over
@theonlychase
theonlychase / remove-duplicates
Created Apr 23, 2019
Keep same order while removing the duplicate
View remove-duplicates
// ES6 Set
function removeDups(arr) {
let noDup = [...new Set(arr)];
return noDup;
}
removeDups(["John", "Taylor", "John"]) // ["John", "Taylor"]
// Reduce
function removeDups(arr) {
let uniq = arr.reduce((a,b) => {
@theonlychase
theonlychase / object-assign
Created Apr 22, 2019
Allows to easily copy values from one object to another. Object.assign becomes really useful to create completely new objects from existing ones, allowing you to copy and expand objects in an immutable manner.
View object-assign
let obj = {
first: 'chase',
last: 'isley',
age: 31,
location: {
city: 'Las Vegas',
state: 'NV'
}
}
@theonlychase
theonlychase / for-of-in
Created Apr 22, 2019
Use for…of to iterate over the values in an iterable, such as an array, string, maps, sets, generators, DOM node collections and arguments object Use for…in to iterate over the properties of an object (the object keys)
View for-of-in
// for-in
let obj = {
name: 'chase',
age: 31,
location: {
city: 'Las Vegas',
state: 'NV'
}
}
@theonlychase
theonlychase / obj-values
Last active Apr 22, 2019
The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
View obj-values
let obj = {
name: 'chase',
age: 31
}
let values = Object.values(obj);
console.log(values) // ["chase", 31]
for (let value of Object.values(obj)) {
console.log(value)
}
View execution-context
## Gobal Execution Context
Execution Contexts allow the JavaScript engine to manage the complexity of interpreting and running your code.
The only time an Execution Context is created is when the JavaScript engine first starts interpreting your code (Global Execution Context) and whenever a function is invoked.
1. How do they get created?
1. The first Execution Context that gets created when the JavaScript engine runs your code is called the “Global Execution Context”.
2. Consists of two things:
You can’t perform that action at this time.