Created
August 8, 2015 06:47
-
-
Save chuck0523/e2b554ad344e5c09f7a8 to your computer and use it in GitHub Desktop.
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
// 全ての関数で利用できるメソッドを追加 | |
Function.prototype.method = function(name, func) { | |
this.prototype[name] = func; | |
return this; | |
}; | |
// 上記メソッドを利用して、小数点切り捨て関数を追加 | |
Number.method('integer', function() { | |
return Math[this < 0 ? 'ceil' : 'floor'](this); | |
}); | |
console.log((-10/3).integer()); // -3 | |
String.method('trim', function() { | |
return this.replace(/^\s+|\s+$/g, ''); | |
}); | |
console.log('"'+' neat '+ '"'); // " neat " | |
console.log('"'+' neat '.trim()+ '"'); // "neat" | |
Function.prototype.method = function (name, func) { | |
if(!this.prototype[name]) { | |
this.prototype[name] = func; | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment