链式调用

67 阅读1分钟

一.add( )( )( )( );

add()()()();
        function add() {
            return function() {
                return function() {
                    return function() {
                    }
                }
            }
        }

二.f1( ).f2( ).f3( );

f1().f2().f3();
        function f1() {
            console.log('f11f1f1f1');
            return {
                f2: function() {
                    console.log('f2f2f2f2');
                    return {
                        f3: function() {
                            console.log('f3f3f3f3');
                        }
                    }
                }
            }
        }

三.f( ).f( ).f( ).f( ).f( ).f( ).f( ).f( ).f( ).f( ).f( );

let x = 0;
        function f() {
            console.log(++x);
            return {
                // f: f,
                // 如果对象中键和值是一样的
                f,
            }
        }

四.

var res1 = arr([47, 22, 58, 10, 0, 100, 79]).max();  // 11
        var res2 = arr([11,2,3]).min();  // 2
        
        function arr(x) {
            // 排序
            x.sort((a, b) => a - b);  // 升序
            return {
                max:function() {
                    return x[x.length - 1];
                },
                min:function() {
                    return x[0];
                },
            }
        }
        console.log(res1, res2);