Javascript

기본 타입에 Method 추가

효락 2017. 12. 1. 17:18

자바스크립트는 기본 타입에 메소드를 추가하여 사용할 수 있다. 


이러한 작업은 함수, 배열, 문자열, 숫자, 정규 표현식, boolean에 모두 가능하다.


Function.prototype.method = function( name, func ){
    if( !this.prototype[name]){
        this.prototype[name] = func;
    }
    return this;
};
 
Number.method('integer'function() {
    return Math[this < 0 ? 'ceil' 'floor'](this);
});
 
console.log((1.333).integer());
console.log((-1.333).integer());