From http://jondavidjohn.com/extend-javascript-functions/