Skip to content

Instantly share code, notes, and snippets.

@liweiz
liweiz / constructor.js
Created January 7, 2017 20:35 — forked from chip/constructor.js
classless javascript
// http://stackoverflow.com/questions/27595749/douglas-crockford-on-class-free-oop-in-javascript
// https://www.youtube.com/watch?v=PSGEjv3Tqo0#t=1395
//
// Per Douglas Crockford (but requries ES6 support)
// function constructor(spec) {
// let {member} = spec,
// {other} = other_constructor(spec),
// method = function () {
// // accesses member, other, method, spec
// };
@liweiz
liweiz / object-creation.md
Created January 7, 2017 20:25 — forked from benpriebe/object-creation.md
Douglas Crockford - Create Object Recipe (2014)

Douglas Crockford showed a slide showing how he creates JavaScript objects in 2014.

He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes.

https://www.youtube.com/watch?v=bo36MrBfTk4 (skip ahead to 35 mins for relevant section)

Here is the pattern described on the slide:

function constructor(spec) {
@liweiz
liweiz / constructor.js
Created January 7, 2017 20:21 — forked from scottcorgan/constructor.js
Douglas Crockford's new constructor without using new
function constructor(spec) {
let { member } = spec,
{ other } = other_constructor(spec),
method = function() {
// member, other, methid, spec
};
return Object.freeze({
method,
other
@liweiz
liweiz / JSONConvenience.swift
Created October 3, 2016 12:57 — forked from griotspeak/JSONConvenience.swift
Convenience overload for JSON parsing
import Foundation
extension JSONSerialization {
enum CastingError : Swift.Error {
case incompatibleType
}
static func jsonObject<T>(with data: Data, options: JSONSerialization.ReadingOptions) throws -> T {
let anyResult: Any = try jsonObject(with: data, options: options)
if let result = anyResult as? T {