Skip to content

Instantly share code, notes, and snippets.

@veggiemonk
Forked from gilbert/functions.js
Created November 11, 2015 10:36
Show Gist options
  • Save veggiemonk/48f698019d657c871624 to your computer and use it in GitHub Desktop.
Save veggiemonk/48f698019d657c871624 to your computer and use it in GitHub Desktop.
JavaScript Function Extensions
// Partially apply arguments to a function. Useful for binding
// specific data to an event handler.
// Example:
//
// var add = function (x,y) { return x + y }
// var add5 = add.papp(5)
// add5(7) //=> 11
//
Function.prototype.papp = function () {
var slice = Array.prototype.slice
var fn = this
var args = slice.call(arguments)
return function () {
return fn.apply(this, args.concat(slice.call(arguments)))
}
}
// Prevents the default action. Useful for preventing
// anchor click page jumps and form submits.
// Example:
//
// var x = 0
// var increment = function () { x += 1 }
// myAnchorEl.addEventListener('click', increment.chill())
//
Function.prototype.chill = function() {
var fn = this
return function(e) {
e.preventDefault()
return fn()
}
}
// Both prevent default and partially apply in one go.
// Example:
//
// var x = 0
// var increment = function (amount) { x += amount }
// myAnchorEl.addEventListener('click', increment.coldPapp(17))
//
Function.prototype.coldPapp = function() {
var slice = Array.prototype.slice
var fn = this
var args = slice.call(arguments)
return function(e) {
e.preventDefault()
return fn.apply(this, args.concat(slice.call(arguments, 1)))
}
}
// Compose two functions together,
// piping the result of one to the other.
// Example:
//
// var double = function (x) { return x * 2 }
// var addOne = function (x) { return x + 1 }
// var doublePlus = double.andThen(addOne)
// doublePlus(3) //=> 7
//
Function.prototype.andThen = function(f) {
var g = this
return function() {
var slice = Array.prototype.slice
var args = slice.call(arguments)
return f.call(this, g.apply(this, args))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment