当前位置 : 主页 > 网络编程 > JavaScript >

非线性参数的函数定义方式

来源:互联网 收集:自由互联 发布时间:2021-06-28
fun.js let fun = function ( a, b, c, d) { let options = { a: 1, b: 'a', c: {}, d: function ( a, b, c) {} }; let isUndefined = o = typeof o == 'undefined'; let isFunction = o = typeof o == 'function'; let isNumber = o = typeof o == 'number';
fun.js
let fun = function ( a, b, c, d) {
    let options = {
        a: 1,
        b: 'a',
        c: {},
        d: function ( a, b, c) {}
    };

    let isUndefined = o => typeof o == 'undefined';
    let isFunction = o => typeof o == 'function';
    let isNumber = o => typeof o == 'number';
    let isString = o => typeof o == 'string';
    let isObject = o => typeof o == 'object';

    let setOptions = function ( o) {
        if ( isNumber( o)) {
            options.a = o;
        } else if ( isString( o)) {
            options.b = o;
        } else if ( isObject( o)) {
            options.c = o;
        } else if ( isFunction( o)) {
            options.d = o;
        }
    }

    for ( let obj of arguments) {
        setOptions( obj);
    }

    options.d( options.a, options.b, options.c);
}

fun( ( a, b, c) => {
    console.log( a, b, c);
}, { list:[]}, 3, 'b');
网友评论