Skip to content

Instantly share code, notes, and snippets.

@liweiz
liweiz / constructor.js
Created Jan 7, 2017 — forked from chip/constructor.js
classless javascript
View constructor.js
// 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 Jan 7, 2017 — forked from benpriebe/object-creation.md
Douglas Crockford - Create Object Recipe (2014)
View object-creation.md

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 Jan 7, 2017 — forked from scottcorgan/constructor.js
Douglas Crockford's new constructor without using new
View constructor.js
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 Oct 3, 2016 — forked from griotspeak/JSONConvenience.swift
Convenience overload for JSON parsing
View JSONConvenience.swift
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 {
You can’t perform that action at this time.