Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
// Proof of concept only!
function isInstance(value, Type) {
if (typeof Type.hasInstance === 'function') {
return Type.hasInstance(value);
} else {
return value instanceof Type;
}
}
isInstance.Primitive = {
Primitive
PrimitiveBoolean
PrimitiveNumber
PrimitiveString
ValueObject
(Future: user-defined value classes)
ReferenceObject
(Objects that are not an instance of Object)
Object
(Subclasses of Object)
@rauschma
rauschma / amd_vs_cjs.js
Created November 15, 2012 18:10
Modules: AMD versus CJS
//---------------------
// AMD
define(["foo", "bar", "baz"], function (foo, bar, baz) {
return {
func: function (...) { ... }
};
});
//---------------------
@rauschma
rauschma / munich.md
Last active April 8, 2018 01:32
A few tips for tourists in Munich
@rauschma
rauschma / global.js
Created December 14, 2013 15:14
Cross-platform way of referring to the global object.
(function (glob) {
// glob points to global object
}(typeof window !== 'undefined' ? window : global));
/**
* Create an array with `len` elements.
*
* @param [initFunc] Optional: a function that returns
* the elements with which to fill the array.
* If the function is omitted, all elements are `undefined`.
* `initFunc` receives a single parameter: the index of an element.
*/
function initArray(len, initFunc) {
if (typeof initFunc !== 'function') {
@rauschma
rauschma / newOperator.js
Created December 11, 2013 23:32
This is roughly how one would implement the `new` operator in JavaScript.
function newOperator(Constr, args) {
var inst = Object.create(Constr.prototype);
var result = Constr.apply(inst, args);
if (typeof result === 'object' && result !== null) {
return result;
}
return inst;
}
const BIG_ENDIAN = Symbol('BIG_ENDIAN');
const LITTLE_ENDIAN = Symbol('LITTLE_ENDIAN');
function getPlatformEndianness() {
let arr32 = new Uint32Array([0x12345678]);
let arr8 = new Uint8Array(arr32.buffer);
switch ((arr8[0]*0x1000000) + (arr8[1]*0x10000) + (arr8[2]*0x100) + (arr8[3])) {
case 0x12345678:
return BIG_ENDIAN;
case 0x78563412:
return LITTLE_ENDIAN;
@rauschma
rauschma / question.md
Last active April 8, 2018 02:25
JavaScript libraries and tools: what are the bare essentials?

Question: If a newcomer to JavaScript asked you for essential libraries and tools, what would you tell them? You don’t want to overwhelm them with too many suggestions!

Focus: language-related functionality (as opposed to browser-related functionality).

  • Libraries: including, say, Underscore.js and promises libraries, but excluding jQuery et al.
  • Tools: package managers, build tools, unit test tools, etc.
    • Less important: editors, IDEs. Rationale: it’s fairly obvious that you need them. There are other tools that people might not even know that they need.