Last active
December 23, 2015 14:49
-
-
Save Leko/6651407 to your computer and use it in GitHub Desktop.
Numberオブジェクトのモンキーパッチ
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
/** | |
* 呼び出された数字をゼロ埋めした文字列を返す | |
* | |
* @method zeroFill | |
* @namespace Number | |
* @param {Number} ゼロ埋めする桁数。呼び出された数字より小さければ無視される | |
* @return {String} ゼロ埋めした文字列 | |
*/ | |
Number.prototype.zeroFill = Number.prototype.zeroFill || function(digit) { | |
"use strict"; | |
var zero = new Array(digit + 1).join("0"), | |
len = this.toString().length; | |
return (zero + this).slice(len < digit ? -digit : -len); | |
}; | |
/** | |
* 呼び出された数字をカンマ区切りにした文字列返す | |
* | |
* @method toCommaString | |
* @namespace Number | |
* @return {String} コンマ区切りの文字列 | |
*/ | |
Number.prototype.toCommaString = Number.prototype.toCommaString || function() { | |
"use strict"; | |
var str = this.toString(), | |
len = str.length, | |
ret = "", | |
i = -1; | |
while(i++, len--) { | |
ret = str[len] + (i && i % 3 == 0 ? ",": "") + ret; | |
} | |
return ret; | |
}; | |
/** | |
* 指定された回数分だけ関数を実行する | |
* | |
* @method times | |
* @namespace Number | |
* @param {Function} fn 実行するコールバック関数 | |
* @return void | |
*/ | |
Number.prototype.times = Number.prototype.times || function(fn) { | |
"use strict"; | |
for(var i = 0; i < this; i++) { | |
fn.call(null, i); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment