Code from obscurejavascript.tumblr.com in an easy to download format
Code from obscurejavascript.tumblr.com in an easy to download format. All of the code can be run in Node.js and most of it in a browser (what can't is noted below). A few posts require libraries and these are found in the node_modules folder and are specified by packages.json. This is a standard NPM layout.
Just like the blog, there may be some examples that do not good follow JavaScript patterns for demonstration purposes.
Each example corresponds directly to a blog post:
new.target
is and why it can be useful.Promise.race
and a simple example Polyfill for it.Promise.allSettled
function that only is fulfilled once all promises run even if there are errors.console.log
's built in formatters and inspector to help with debugging.Promise.any
and a simple example Polyfill for it.String.repeat
can be used to easily create nicely formatted console messages.URL
object can be used to greatly simplify URL string operations.var
is still necessary in some cases even though let
exists.async
function declarations can simplify async code. Node 8+/Modern browsers only
async
helps with conditional logic in asynchronous requests.async
function can be used in loops.class
syntax simplifies inheritanceclass
keyword in JavaScript.Intl
object.toFixed
can be used to implement rounding and its other uses.String.indexOf
and how it is useful beyond performance optimization.valueOf
property. Newer syntax added since the 2017 post and a much better introduction.this
based functions easier to work with and can make them borrowable.for...of
loops are.let
and const
in JS.Map
object and how it can be useful compared to plain objectsnew
works with a polyfill-like implementation.__proto__
and prototype
and their usage.Promise.all
.util.promisify
can be used to transform async functions into promises.Reflect
API can be used as a single interface for many general JS operations.this
based interfaces into parameter-only ones. The tactic works in any environment, but the examples are Browser only.global
with window
and it will work with very modern browsers.applyApiCalls.js: How Array.apply can be used as a general pattern to add more functionality to base API calls.
assignTo.js: A function to drastically reduce syntax for asynchronous object property updates.
at.js: A function to reduce the time to get values from potentially empty deep objects.
attempt.js: A function to simplify try and catch. See the corresponding blog post for why this is useful.
bitwise.js A simple example of when bitwise operations can lead to large performance advantages.
callLater.js: A function to store functions to be called later.
callbackWith.js: A way to simplify unit tests by making a function that provides a quick way to mock out functions that call calbacks. Also includes an argsAtSync utility.
compose.js: A function used to simplify sequences of functions.
curry.js: A function that only gets called when passed all arguments otherwise it returns a curry.
extendLodash.js: How to extend Lodash with details for chaining and how it is used across imports.
flatMap.js: A map function that allows multiple values or no values to be returned for each array element. Allows returns that are shorter than the original array.
functionToString.js: About the Function.toString method and how it can be used for debugging and function regeneration.
generator.js: How to significantly reduce the number of callbacks for asynchronous operations by using generators.
getPagePosition.js: How to get an HTML elements position in the page no matter where it is. Browser Only
getters.js: Getters and Setters similar to other Object Oriented Languages in JavaScript.
groupBy.js: An array of object grouping function and what it can be applied to.
identity.js: How the identity function can be useful.
intersection.js: A function that returns the common elements of 2 array, gives an example of where this is useful.
is.js: How a function to check object type is useful.
objectTypeAndEquality.js: When objects with identical properties and printouts are not equal.
observable.js: How to treat data that arrives over time in an array like fashion.
[objectFreeze.js]((http://obscurejavascript.tumblr.com/post/152213315637/constants-and-objectfreeze-in-javascript): How to use const and Object.freeze to create completely immutable objects.
objOf.js: A shorthand way to create objects with dynamic property names in pre-es6 JavaScript.
oneLineForLoop.js: An explanation of for loop structure by using one line for loops only.
mapJoin.js: Using the abstraction of splitting then mapping then joining to simplify string set manipulation.
mapValues.js: Object traversal operations (mapValue and mapKeys) that iterate through objects like Array.map.
maxDate.js: Using one line of code to find the maximum date.
negate.js: How a simple closure based negate function can simplify code.
mixins.js: A simple way to implement mixins in JavaScript.
nonMutatedLists.js: A simple way to make list operations not mutate their lists.
multiLineJoinString.js: Less cumbersome syntax for multi-line strings in ES5 or less environments.
lazyObject.js: Creating objects that can create more objects or can just be used with no instantiation.
propertyMap.js: A function to group properties from a set of objects. Also has a tactic to avoid array.push in maps.
proxy.js: How to change JavaScript syntax on given objects via the Proxy API.
proxyNumberValidation.js: How to implement JS type checking without setters by using the Proxy API.
publishSubscribeAutomation.js: How to use an abstracted publisher to implement more advanced publish-subscribe patterns.
replace3rdArg.js: How to use the little known ability of String.replace to accept callbacks.
returnWrap.js: How a return wrapper function can be used to merge objects that chain.
reverseLater.js: Return a function that calls the passed in callback with reversed arguments.
sealAndPreventExtensions.js: The difference between the various methods to make objects constant in JavaScript.
selfRegeneration.js: How an object can regenerate itself. This is useful when the object constructor is lost.
seriesGeneration.js: Simplifying series generation via Functional Programming principles.
stoppingForEachLoops.js: How to stop forEach loops without using an exception and a better strategy.
stub.js: A simple one function method of stubbing functions including asynchronous ones.
stringify.js: How to use stringify to pretty print JavaScript objects in a customizable way.
tap.js: How to use the tap and thru methods to customize chained behavior and make debugging much easier.
timing.js: The various ways of timing function calls in JavaScript. Starting with the oldest methods.
quickObj.js: A recursive function to quickly create high depth objects for unit and other tests.
void.js: Uses of the void keyword. Best understood in conjunction with the blog post in this case.
zipObj.js: A function that maps a list of keys to a list of values. This shows an example of why that is important.
argumentParameterBinding.js: A demonstration of a little known arguments object quirk.
arrowFunctions.js: How to implement arrow functions in pre ES6 JavaScript.
beforeAndAfter.js: A before function with an extension into an after one.
bind.js: Bind and an example of it in JavaScript.
chainify.js: How to build a generic chaining method that can add chaining to any object.
circularReference.js: Circular references and potential issues with them. Does not go into all the details, more of an overview.
circularReferencePositives.js: Positives of circular references.
class.js: Classes in ES6 and how to create a close equivalent.
clone.js: How to deep copy objects in JavaScript.
const.js: The const keyword in ES6 and how to create a close equivalent.
debounce.js: Create a wrappped function so that the function it wraps is only called after it stops being triggered every n milliseconds.
defer.js: Run a function after all processing is done in the current callstack.
dependencyInversion.js: Screwed up the monad post. So just the dependency inversion part will be kept.
floatingPointArithmetic.js: A simple algorithm strategy for reliably calculating decimal based numbers.
flow.js: This is used to string functions that use each others results in a chain.
functionDecompilation.js: Function decompilation, Angular.js and simplified dependency injector for it.
functionExpressionProperties.js: How properties on object can be used to simplify code. Specifically, with named Function Expressions.
generator.js: How to implement a relatively close equivalent of generators in pre ES6 JavaScript.
genericChaining.js: An explanation of a simple generic pattern for creating chainable objects.
interatedAsyncCalls.js: How to solve a seemingly wierd issue while passing state to multiple asynchronous calls in JavaScript.
iterator.js: A simple implementation of the ES6 iterator abstraction in non ES6 JavaScript (without an Iterable implementation).
lazyFunctionDefinition.js: Caching values without using conditionals. This is useful for calls that need to be made multiple times.
mapObject.js: An implementation of the map object in ES6 in less than ES6 JavaScript.
mapOrRemove.js: A function used to filter and modify objects.
mapsThirdArgument.js: How the 3rd argument in map() can be used to operate on multiple array elements at once.
memoize.js: A way to automatically cache function calls.
nonMutatingAssign.js: Showing the implementation of a function similar to Lodash assign and how to not mutate with it.
oneLineRandomText.js: How to generate short random text codes in JavaScript in one line using no library functions.
sortsAndNan.js: What NaN return values do to JavaScript's default sort.
templateStrings.js: Evaluating strings with expressions in JavaScript similar to ES6 templates.
let.js: The ES6 let statement and how to emulate close aproximations.
monad.js: Defining a chainable specification of operations.
multiIf.js: Making complex conditionals more readable by using an expression and chaining conditions.
multicore.js: Multicore node.js code. Node.js Only
omit.js: Return an object with properties removed with no side effects.
primitiveCoercion.js: A brief explanation of automatic primitive coercion in JavaScript.
promise.js: Create an object that represents future state.
undefined.js: An explanation of the undefined type.
repl.js: A REPL of JavaScript in JavaScript using streams. Node.js Only
server.js: A 4 line http server using connect.js and serve-static.js Node.js Only
setObject.js: An implementation of the set object in ES6 in less than ES6 JavaScript.
stream.js: A simple example of streams Node.js Only
symbol.js: Implementing ES6 Symbols in pre ES6 syntax.
thisKeyword.js: An attempt of defining all the possible ways that the this context is defined.
throttle.js: Create a wrappped function so that the function it wraps is called at most every n milliseconds.
trim.js: The trim string function and polyfills for trimLeft and trimRight.
typedArray.js: A simple demonstration of the differences between Array and a Typed Array.
where.js: Code for the JavaScript where function.
unionAndIntersection.js: Code for the intersection and union array manipulation functions and the unifyling function.
variableChaining.js: How variable chaining can create globals and how global creation can be avoided.
zip.js: How to use a function that automatically structures raw array data.