Skip to content

Instantly share code, notes, and snippets.

View mdix's full-sized avatar
🇩🇰

Marc Dix mdix

🇩🇰
View GitHub Profile
@mdix
mdix / getBaseClass.js
Last active October 16, 2019 11:23
Determine base class (last level before builtin)
function getBaseClass(MaybeClass) {
if (typeof MaybeClass !== 'function') {
return null;
}
const Proto = Reflect.getPrototypeOf(MaybeClass);
return !Proto.name
? MaybeClass
: getBaseClass(Proto);
@mdix
mdix / xhr.js
Created November 15, 2016 14:56
XHR no connection wrapper
var req = new XMLHttpRequest();
req.open("post", 'https://jquery.com', true);
req.onreadystatechange = function receiveResponse() {
console.log('readyState: ', this.readyState);
console.log('status: ', this.status);
console.log('response: ', this.response.length);
if (this.readyState == 4) {
if (this.status === 0 && this.response.length === 0) {
return console.log('NO CON')
@mdix
mdix / mixin.js
Created August 25, 2016 12:21
Mixin that doesn't know anything about the object it's mixed into. Check http://disq.us/p/1ba1awd for discussion
var Person = {
init: function(name) {
this.name = name;
},
doSomethingAndUseSpeak: function(text) {
this.speak({name: this.name, text})
}
};
var canSpeak = {
// 9: object-literals - basics
// To do: make all tests pass, leave the assert lines unchanged!
describe('The object literal allows for new shorthands', () => {
const x = 1;
const y = 2;
describe('with variables', () => {
it('the short version for `{x: x}` is {x}', () => {
@mdix
mdix / email.txt
Last active June 12, 2016 11:18
E-Mail from Chad Parry
That looks great. Sorry it took me forever to take a look at it.
One pedantic thing that I would change is that I would aim for immutability whenever possible.
For example, ApplicationScope.setArgs (https://github.com/mdix/diy-di-typescript/blob/master/class/ApplicationScope.ts#L4)
wouldn't be necessary if instead the ApplicationScope took the args in the constructor.
That way, you are guaranteed that everything is initialized correctly throughout the entire program.
Another pedantic thing I would watch out for is to purge your scope class of any logic.
That includes array access, (https://github.com/mdix/diy-di-typescript/blob/master/class/Injector.ts#L17).
Even though "getArgs()[0]" looks harmless, it's the beginning of an ArgumentParser class, and so that's where that logic usually belongs.
Otherwise, more and more logic will creep in to your scope object.None of that logic will be testable.
On the other hand, keeping the logic in an ArgumentParser class means you can test the corner cases, such a
@mdix
mdix / yay.js
Last active December 4, 2015 11:15
works
'use strict';
var stampit = require('stampit');
var myStamp =
stampit().
init(() => {
var privateMethod = () => {
console.log('YAY');
}
@mdix
mdix / priv_pub.js
Created December 4, 2015 10:20
stampit access private from public
myStamp =
stampit().
init(() => {
this.privateMethod = () => {
// private method
}
})
.methods({
publicMethod: () => {
// consume privateMethod here
@mdix
mdix / app.js
Last active November 17, 2015 10:00
Interface check for stampit 2
'use strict';
var stampit = require('stampit');
const MyInterfaceCheckStamp =
stampit()
.init(function(stamp) {
for (var method in stamp.stamp.fixed.methods) {
var currentMethod = stamp.stamp.fixed.methods[method];
@mdix
mdix / quick.js
Created November 17, 2015 09:11
Something
stampit()
.init(function() {
if (this.connect.toString().indexOf('not implemented') !== -1) {
this.connect();
}
if (this.save.toString().indexOf('not implemented') !== -1) {
this.save();
}
})
.methods(
'use strict';
const stampit = require('stampit');
var Wheels =
stampit()
.props(
{
amount: 4,
diameter_inch: 17,
pressure_psi: 2.1