Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 全ての関数で利用できるメソッドを追加
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