Last active
August 29, 2015 14:04
-
-
Save pedroassis/9ab28b46d32e70882a5a to your computer and use it in GitHub Desktop.
Functional way for a if statement
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
// Just to be sure ;) | |
if (!Boolean.prototype.is) | |
{ | |
Boolean.prototype.is = function(fun /*, thisArg */) | |
{ | |
'use strict'; | |
if (this === void 0 || this === null){ | |
throw new TypeError(); | |
} | |
var type = typeof fun; | |
if (type !== 'function'){ | |
throw new TypeError(type + " is not a function"); | |
} | |
var thisArg = arguments.length >= 2 ? arguments[1] : void 0; | |
if(this){ | |
return fun.call(thisArg); | |
} | |
return this; | |
}; | |
} | |
var aBool = true; | |
if(aBool){ | |
alert("it is"); | |
} | |
// Even better | |
aBool.is(function(){ | |
alert("It is better ?"); | |
}); | |
// Chaining possible | |
aBool.is(function(){ | |
alert("It is better ?"); | |
return ttrue; | |
}).is(function(){ | |
alert("It is worse ?"); | |
return false; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment