Skip to content

Instantly share code, notes, and snippets.

View mpj's full-sized avatar

Mattias Petter Johansson mpj

View GitHub Profile
@mpj
mpj / snake2.js
Last active December 30, 2023 03:07
<title>hello!!</title>
<canvas
name=canvas
width=300 height=300 style="border: solid 2px red">
</canvas>
<script>
canvas = document.body.children[0]
context = canvas.getContext('2d')
@mpj
mpj / classless.md
Last active November 13, 2023 16:34

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 / example01.js
Created August 14, 2017 07:38
Code for the async/await episode of Fun Fun Function.
const response = await fetch(`https://catappapi.herokuapp.com/users/${userId}`)
const data = await response.json()
return data.imageUrl
}
@mpj
mpj / order-total.js
Created November 26, 2017 21:03
Code from Unit testing in JavaScript Part 3 - Test runners
function orderTotal(order) {
return order.items.reduce((prev, cur) => cur.price * (cur.quantity || 1) + prev, 0)
}
module.exports = orderTotal
@mpj
mpj / 0-array.js
Last active May 25, 2023 15:48
Code to the video - "Map: Part 2 of Functional Programming in JavaScript"
var animals = [
{ name: 'Fluffykins', species: 'rabbit' },
{ name: 'Caro', species: 'dog' },
{ name: 'Hamilton', species: 'dog' },
{ name: 'Harold', species: 'fish' },
{ name: 'Ursula', species: 'cat' },
{ name: 'Jimmy', species: 'fish' }
]
@mpj
mpj / SocketServer.java
Created May 3, 2015 08:12
Java/Node socket client interaction
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 / watcher.rb
Last active January 26, 2023 01:05
Keep directory in sync with remote server using Rsync and FSEvent.
#!/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 / episode.js
Created January 15, 2017 21:14
Code from the "Dependency Injection Basics" episode of Fun Fun Function
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', () => {
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
function Person(saying) {
this.saying = saying
}
Person.prototype.talk = function() {
console.log('I say:', this.saying)
}
function spawn(constructor) {
var obj = {}