最近看到2种不错的JavaScript的匿名函数写法,mark一下

写法一:多级函数调用

function xhr(respond) {
    return function(options) {
        return options.m + options.n + respond.h
    }
}

function xhr({h : 1})({m: 2, n: 3})
// 6

写法二: 函数类型匹配

var Handler = {
    extend: function(obj) {
        return obj.m * obj.n
    }
}

Handler.gen = function(obj) {
    return obj.m + obj.n
}

function xhr(type) {
    if (Handler[type]) {
        return Handler[type]({
            m: 2,
            n: 3
        })        
    }
    return 0
}

xhr('gen') // 5
xhr('extend') // 6

这两种基本写法还可以随便扩展