Skip to content

Instantly share code, notes, and snippets.

@rauschma
rauschma / super.js
Created Nov 1, 2011
Semi-dynamic super references in JavaScript
View super.js
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 Nov 3, 2011
A minimal class proposal for ECMAScript.next
View class.js
// 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 Apr 8, 2018
Static super references in JavaScript
View static_super.js
// 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 Nov 17, 2011
A synthesis of ECMAScript.next class proposals
View js_inheritance.md

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 Jan 23, 2012
Triple module boilerplate: Node.js, AMD, plain browser
View module_boilerplate.js
// 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
Class declarations with object exemplar semantics
View object_exemplar_decls.js
//----- 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 Aug 10, 2012
Node.js gets stuck reading stdin if a line has more than 1024 characters
View nodejs-stdin.js
#!/usr/bin/env node
process.stdin.resume();
process.stdin.setEncoding('utf8');
var str = "";
process.stdin.on('data', function (chunk) {
str += chunk;
});
View iframe_test.html
<!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
View at_operator.js
// 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 Oct 30, 2012
Universal Module Definition pattern
View umd_pattern.js
// 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) {