js 函数式用法(0)

我们两清 提交于 2020-02-04 15:15:08

Function.prototype.method = function (name, func) {
       this.prototype[name] = func;
       return this;
 };


Number.method('integer', function ( ) {
       return Math[this < 0 ? 'ceil' : 'floor'](this);
    });
    
document.writeln((-10 / 3).integer( )); // -3
 

String.method('trim', function ( ) {
       return this.replace(/^\s+|\s+$/g, '');
    });
    
document.writeln('"' + " neat ".trim( ) + '"');

 

上述3段代码 展现了 函数式编程的魅力之一

函数1在初始函数处定义了一个函数 函数赋值给函数本体的 参数函数

函数2在定义的函数 因为在初始函数定义了method  所以 可以直接调用 并传递 函数到 prototype 中

函数3同理
 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!