Skip to content

Instantly share code, notes, and snippets.

Avatar

Mattias Petter Johansson mpj

View GitHub Profile
@mpj
mpj / classless.md
Last active February 28, 2023 15:39
View classless.md

The future is here: Classless object-oriented programming in JavaScript.

Douglas Crockford, author of JavaScript: The Good parts, recently gave a talk called The Better Parts, where he demonstrates how he creates objects in JavaScript nowadays. He doesn't call his approach anything, but I will refer to it as Crockford Classless.

Crockford Classless is completely free of class, new, this, prototype and even Crockfords own invention Object.create.

I think it's really, really sleek, and this is what it looks like:

function dog(spec) {
@mpj
mpj / SocketServer.java
Created May 3, 2015 08:12
Java/Node socket client interaction
View SocketServer.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static final int port = 12345;
private ServerSocket server;
public void listen() {
@mpj
mpj / order-total.js
Created November 26, 2017 21:03
Code from Unit testing in JavaScript Part 3 - Test runners
View order-total.js
function orderTotal(order) {
return order.items.reduce((prev, cur) => cur.price * (cur.quantity || 1) + prev, 0)
}
module.exports = orderTotal
@mpj
mpj / watcher.rb
Last active January 26, 2023 01:05
Keep directory in sync with remote server using Rsync and FSEvent.
View watcher.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'rb-fsevent'
require 'ruby-growl'
def sync(local, host, remote, growl)
# Construct the bash command that runs rsync.
@mpj
mpj / example01.js
Created August 14, 2017 07:38
Code for the async/await episode of Fun Fun Function.
View example01.js
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`)
const data = await response.json()
return data.imageUrl
}
@mpj
mpj / episode.js
Created January 15, 2017 21:14
Code from the "Dependency Injection Basics" episode of Fun Fun Function
View episode.js
const assert = require('assert')
function getAnimals(fetch, id) {
return fetch('http://api.animalfarmgame.com/animals/' + id)
.then(response => response.json())
.then(data => data.results[0])
}
describe('getAnimals', () => {
it('calls fetch with the correct url', () => {
View order-total.js
function orderTotal(fetch, order) {
fetch('https://vatapi.com/v1/country-code-check?code=' + order.country)
return Promise.resolve(order.items.reduce((prev, cur) =>
cur.price * (cur.quantity || 1) + prev, 0))
}
module.exports = orderTotal
@mpj
mpj / example.js
Created September 18, 2016 14:08
Code from Fun Fun Function YouTube episode: The 'new' keyword - Object Creation in JavaScript P3
View example.js
function Person(saying) {
this.saying = saying
}
Person.prototype.talk = function() {
console.log('I say:', this.saying)
}
function spawn(constructor) {
var obj = {}
@mpj
mpj / monad-stream-example.js
Last active September 14, 2022 06:42
This is the code from Monads - episode #21 of FunFunFunction (https://www.youtube.com/playlist?list=PL0zVEGEvSaeFSwPn06GKArptSxiP1Gff8)
View monad-stream-example.js
const fetch = require('node-fetch')
const Bacon = require('baconjs')
function getInPortuguese(word) {
// Google Translate API is a paid (but dirt cheap) service. This is my key
// and will be disabled by the time the video is out. To generate your own,
// go here: https://cloud.google.com/translate/v2/getting_started
const apiKey =
'AIzaSyB4DyRHIsNhogQXmH16YKbZfR-lTXrQpq0'
const url =
View index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Intro to XState</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="box"></div>