Created
May 22, 2015 13:13
-
-
Save truetone/c2c8c121dbfc26e776f9 to your computer and use it in GitHub Desktop.
Handy JavaScript Functions
This file contains hidden or 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
/** | |
* JavaScript's typeof method returns "object" for arrays and null values. This fixes that. | |
* http://javascript.crockford.com/remedial.html | |
*/ | |
function typeOf(value) { | |
var s = typeof value; | |
if (s === 'object') { | |
if (value) { | |
if (value instanceof Array) { | |
s = 'array'; | |
} | |
} else { | |
s = 'null'; | |
} | |
} | |
return s; | |
} | |
/** | |
* Evaluates any JavaScript object that is a Number to see if it is a | |
* multiple of the number passed | |
* @example | |
* // returns true | |
* 15.isMultipleOf(5); | |
* @method | |
* @param {Number} | |
* @returns {Boolean} | |
*/ | |
Number.prototype.isMultipleOf = function(n) { | |
return (this % n) === 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment