Created
May 24, 2012 20:25
-
-
Save cowboy/2784002 to your computer and use it in GitHub Desktop.
A modern JavaScript if-elseif-else abstraction, because control flow statements are so 1995
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Abstraction.js | |
* | |
* Copyright (c) 2012 "Cowboy" Ben Alman | |
* Licensed under the MIT license. | |
* http://benalman.com/about/license/ | |
*/ | |
var Abstraction = (function($) { | |
var _ = $.prototype; | |
_.then = function(_) { | |
this._[0] && !this._.slice(1).some(Boolean) && _(); | |
return this; | |
}; | |
_.if = _.$if = function(_) { | |
this._ = [_]; | |
return this.then; | |
}; | |
_.elseif = _.$elseif = function(_) { | |
this._.unshift(_); | |
return this.then; | |
}; | |
_.else = _.$else = function(_) { | |
this._.some(Boolean) || _(); | |
return this; | |
}; | |
return $; | |
}(function() { | |
this.then = this.then.bind(this); | |
this.then.then = this.then; | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Working on the "chain" gang | |
(new Abstraction) | |
.if(true).then(function() { | |
console.log('log 1'); | |
}) | |
.if(false).then(function() { | |
console.log('no log'); | |
}) | |
.if(true).then(function() { | |
console.log('log 2'); | |
}) | |
.else(function() { | |
console.log('no log'); | |
}) | |
.if(false).then(function() { | |
console.log('no log'); | |
}) | |
.else(function() { | |
console.log('log 3'); | |
}) | |
.if(true).then(function() { | |
console.log('log 4'); | |
}) | |
.elseif(true).then(function() { | |
console.log('no log'); | |
}) | |
.else(function() { | |
console.log('no log'); | |
}) | |
// You actually don't even need the .then! | |
.if (false) (function() { | |
console.log('no log'); | |
}) | |
.elseif (true) (function() { | |
console.log('log 5'); | |
}) | |
.else (function() { | |
console.log('no log'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For the sake of this example looking totally insane, this function returns the | |
// console.log function with all arguments pre-applied, to be called at a later time. | |
function console$log() { | |
return Function.bind.apply(console.log, [console].concat([].slice.call(arguments))); | |
} | |
// Dolla dolla "with" y'all (aka WTF) | |
with (new Abstraction) { | |
$if (true) ( | |
console$log('log 1') | |
) | |
$if (false) ( | |
console$log('no log') | |
) | |
$if (true) ( | |
console$log('log 2') | |
) | |
$else ( | |
console$log('no log') | |
) | |
$if (false) ( | |
console$log('no log') | |
) | |
$else ( | |
console$log('log 3') | |
) | |
$if (true) ( | |
console$log('log 4') | |
) | |
$elseif (true) ( | |
console$log('no log') | |
) | |
$else ( | |
console$log('no log') | |
) | |
$if (false) ( | |
console$log('no log') | |
) | |
$elseif (true) ( | |
console$log('log 5') | |
) | |
$else ( | |
console$log('no log') | |
) | |
} |
Too verbose. This should really be supported by syntax.
I'm pushing for ES.prev, a return to ECMAScript basics.
Reminds me of io, where control structures aren't special at all.
👍. This really needs to be a jQuery plugin, though.
this looks awesome :)
At least you get scoping! (also, you're crazy).
Yeah, now you can have "block" scope in JavaScript without those disgusting IIFEs!
i like this code!
This form resembles traditional ifs more... ;)
I've actually added support for 2 different syntaxes. Chaining and WTF.
Looks nice, but i'm afraid not usable with CoffeeScript.
Also see the other version, Abstraction.js "Lite".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(I've clearly put a lot of thought into this)