Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created August 8, 2015 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/e2b554ad344e5c09f7a8 to your computer and use it in GitHub Desktop.
Save chuck0523/e2b554ad344e5c09f7a8 to your computer and use it in GitHub Desktop.
// 全ての関数で利用できるメソッドを追加
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