Skip to content

Instantly share code, notes, and snippets.

@spikesagal
spikesagal / property-mixer.js
Last active November 3, 2015 23:48
A safer way to mix JSON objects to be used as config or POST data.
function mixin() {
return JSON.parse('{'
+ [].reduce.call(arguments, function (prev, cur) {
var str = JSON.stringify(cur);
if (!str || str.slice(0, 1) !== '{' || str.slice(-1) !== '}') {
throw new Error('Invalid object: ' + str);
}
return (prev ? prev + ',' : '')
+ str.slice(1, -1);
}, null)
@spikesagal
spikesagal / run.js
Created October 1, 2015 17:01
ES6 destructure named args into properties of constructed object (this).
'use strict';
class Person {
constructor(arg = {
firstName: 'John',
lastName: 'Doe'
}) {
({
firstName: this.firstName = '<FIRSTNAME_UNKNOWN>',
lastName: this.lastName = '<LASTNAME_UNKNOWN>'
} = arg);
@spikesagal
spikesagal / run.js
Created September 30, 2015 22:05
Self-incrementing, read-only counter property; prototype linking version.
(function IIFE() {
'use strict';
function counter() {
this.def = 0;
return (function() {
return this.def++;
}).bind(this);
};
var obj = {};
Object.defineProperties(obj, {
@spikesagal
spikesagal / run.js
Created September 30, 2015 15:18
Self-incrementing, read-only counter property
(function IIFE() {
'use strict';
var obj = {};
Object.defineProperties(obj, {
counter: {
get: (function IIFE() {
var def = 0;
return function() {
return def++;
}