null object Design pattern
by dimitrs_papadimitriou
HTML
<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
JavaScript
var realObject = () => ({
operation: () => {console.log("Real Object");}
});
var nullObject = () => ({
operation: () => { }
});
//classical null checking
var result = null;
if (result != null) {
result.operation();
} else {
//do nothing
}
//strategy like null managment dose not alter the structure
var result = realObject();
result.operation();
var some = (v) => ({
v: v,
map: (f) => some(f(v))
});
var none = () => ({
v: null,
map: (f) => none()
});
var s = some(5).map(x => x * 5);