Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
@rauschma
rauschma / super.js
Created November 1, 2011 20:07
Semi-dynamic super references in JavaScript
function A() {
}
A.prototype.desc = function() {
return "A";
}
function B() {
}
B.prototype = Object.create(A.prototype);
B.prototype.desc = function() {
@rauschma
rauschma / class.js
Created November 3, 2011 15:52
A minimal class proposal for ECMAScript.next
// Roughly: a combination of Jeremy’s and Allen’s ideas, updated with the results of recent discussions
// Guidelines:
// - Don’t use the same syntax as for object literals, but stay close.
// - Rationale: Some object literal features are forbidden in class declarations => don’t confuse people
// - Rationale: Comma separation is a bit tricky.
// - Keep new features at a minimum
// - Don’t obscure the fact that private names are actually name objects.
// => They can also be imported from somewhere else – a use case that needs to be supported.
// - In order to minimize confusion, keep module syntax and class declaration syntax similar.
@rauschma
rauschma / static_super.js
Last active April 8, 2018 02:25
Static super references in JavaScript
// Simulated static super references (as proposed by the current draft of the ECMAScript 6 specification)
//------------------ Library
function inherits(subC, superC) {
var subProto = Object.create(superC.prototype);
// At the very least, we keep the "constructor" property
// At most, we preserve additions that have already been made
copyOwnFrom(subProto, subC.prototype);
setUpHomeObjects(subProto);
@rauschma
rauschma / js_inheritance.md
Created November 17, 2011 19:43
A synthesis of ECMAScript.next class proposals

A minimal class proposal

Given that most things that matter at “class” level also matter at object level, I still feel that using object initializer notation for class declarations is the simplest solution. The following is a synthesis of some of the proposals out there.

  1. Introduce Allen’s class operator as a declarative statement and an expression (similar to Jeremy’s and David’s proposals). IMHO, that’s best when it comes to ease of use for newbies.

     class C { ... }
     class C extends B { ... }
    

let C = class { ... };

@rauschma
rauschma / module_boilerplate.js
Created January 23, 2012 15:36
Triple module boilerplate: Node.js, AMD, plain browser
// No imports, those are more work, especially for plain browser.
// However, as soon as you have imports, you should switch to AMD on browsers.
// Related: http://www.2ality.com/2011/11/module-gap.html
({ define:
typeof define === "function" ?
define
: typeof module !== "undefined" ?
function(F) { module.exports = F() }
: function(F) { this.defClass = F() }.bind(this)
@rauschma
rauschma / object_exemplar_decls.js
Created May 13, 2012 04:23
Class declarations with object exemplar semantics
//----- Example code:
// Class declarations:
// - No data properties allowed
// - Future: support mixins
// - Optional: call the initialization method `new` instead of `constructor`
// Superclass
class Person {
constructor(name) {
@rauschma
rauschma / nodejs-stdin.js
Created August 10, 2012 03:16
Node.js gets stuck reading stdin if a line has more than 1024 characters
#!/usr/bin/env node
process.stdin.resume();
process.stdin.setEncoding('utf8');
var str = "";
process.stdin.on('data', function (chunk) {
str += chunk;
});
@rauschma
rauschma / iframe_test.html
Created August 28, 2012 23:29
iframe test
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>instanceof test</title>
<script>
// test() is called from the iframe
function test(arr) {
var iframeWin = frames[0];
console.log(arr instanceof Array); // false
@rauschma
rauschma / at_operator.js
Created August 28, 2012 23:57
@ operator
// Based on: http://wiki.ecmascript.org/doku.php?id=strawman:syntactic_support_for_private_names
//// The at (@) operator
// Intuitively:
// - obj.@foo is syntactic sugar for obj[foo]
// - @foo is syntactic sugar for [foo] (for method definitions, property definitions, etc.)
// Usage: Let propName be a normal variable holding either a symbol or a string.
@rauschma
rauschma / umd_pattern.js
Created October 30, 2012 21:44
Universal Module Definition pattern
// Related: UMD, a project cataloging UMD patterns https://github.com/umdjs/umd
// Universal module definition: requires either an AMD loader or Node.js
({ define: typeof define === 'function'
? define
: function (names, body) {
module.exports = body.apply(null, names.map(require));
}
}).
define(["foo", "bar"], function (foo, bar) {