Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
@rauschma
rauschma / umd.js
Last active April 8, 2018 02:26
My version of the UMD pattern. https://github.com/umdjs/umd
if (typeof define !== 'function') {
// Not AMD
if (typeof require === 'function') {
// Node.js
var define = function (body) {
module.exports = body(require);
};
} else {
// Vanilla browser
var define = function (body) {
// Anti-pattern: creating a new promise instead of continuing an existing chain
insert() { // method inside ES6 class
return new Promise((resolve, reject) => {
this.db.collection(this.collection).insert(this.fields)
.then((modelDocument) => {
this.fields = modelDocument;
this.notifyObservers({event: "created", model: this});
resolve(modelDocument)
}).catch((err) => {
reject(err)
@rauschma
rauschma / PreventUnknownGet.js
Last active August 12, 2021 16:07
Prevent getting of unknown properties via ES6 proxies
// The following code is valid ECMAScript 6, but doesn’t work in Firefox, yet
function PreventUnknownGet() {
}
PreventUnknownGet.prototype = new Proxy(Object.prototype, {
get(target, propertyKey, receiver) {
if (!(propertyKey in target)) {
throw new TypeError('Unknown property: '+propertyKey);
}
// Make sure we don’t block access to Object.prototype

The term protocol is highly overloaded in computer science. One definition (related to the concept of “metaobject protocol”) is:

A prototcol is about achieving tasks via an object, it comprises a set of methods plus a set of rules for using them.

Note that this definition is different from viewing protocols as interfaces (as, for example, Objective C does), because it includes rules.

@rauschma
rauschma / proxies-es5.js
Last active November 2, 2020 23:16
ES6 proxies in ES5
//----- The ECMAScript 6 meta object protocol (MOP) implemented in ES5
// This is how getting a property is handled internally.
// Double underscore (__) implies internal operation.
Object.prototype.__Get__ = function (propKey, receiver) {
receiver = receiver || this;
var desc = this.__GetOwnProperty__(propKey);
if (desc === undefined) {
var parent = this.__GetPrototypeOf__();
if (parent === null) return undefined;
@rauschma
rauschma / deploying_es6.md
Last active April 8, 2018 02:26
Options for deploying ES6 code

This is a rough overview of the options for deploying ES6 to current JavaScript environments. Not everything can be combined with everything:

  • Decide on a transpiler (for your code):
    • TypeScript
    • Traceur
    • 6to5
  • Decide on a package manager (for existing libraries):
    • npm
    • Bower
  • jspm
@rauschma
rauschma / es6_construct.js
Last active April 8, 2018 02:26
Constructor-calls in ECMAScript 6
// Simplified version of Sect. 9.2.3
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ecmascript-function-objects-construct-argumentslist-newtarget
/**
* All constructible functions have this own method,
* it is called by the `new` operator
*/
AnyFunction.__Construct__ = function (args, newTarget) {
let Constr = this;
let kind = Constr.__ConstructorKind__;
function* zip(...iterables) {
let iterators = iterables.map(i => i[Symbol.iterator]());
while (true) {
let entries = iterators.map(i => i.next());
let done = entries.some(entry => entry.done);
if (done) break;
yield entries.map(e => e.value);
}
}
@rauschma
rauschma / jspm.md
Last active April 8, 2018 02:26
jspm

What is jspm?

Old description

  • jspm is a package manager for the SystemJS universal module loader, built on top of the dynamic ES6 module loader

  • Load any module format (ES6, AMD, CommonJS and globals) directly from any registry such as npm and GitHub with flat versioned dependency management. Any custom registry endpoints can be created through the Registry API.

  • For development, load modules as separate files with ES6 and plugins compiled in the browser.

@rauschma
rauschma / es6_influences.md
Last active April 8, 2018 02:26
ECMAScript 6 influences

Technologies that influenced ES6 features:

  • Generators: Python
  • Arrow functions: CoffeeScript
  • const: C++ (the latest C standard has borrowed it from C++)
  • let: is old, became popular via BASIC.
    • Also frequently appears in functional programming languages (Lisp, ML, etc.), but creates immutable bindings there.
  • Template literals: E (quasi literals)
  • Destructuring: Lisp (destructuring bind)
  • Modules: CommonJS, AMD