在Underscore.js中递归/深度扩展/分配?

8 浏览
0 Comments

在Underscore.js中递归/深度扩展/分配?

有没有办法让Underscore.js的extend函数递归地工作?

实际上,在creditOperation中的query属性将完全覆盖在baseOperation中定义的query属性:

var url = require('url')
  , _ = require('underscore'),
  , baseOperation = {
        host: 'gateway.skebby.it',
        pathname: 'api/send/smseasy/advanced/http.php',
        protocol: 'https',
        query: {
            'username': 'foo',
            'password': 'bar',
        }
    };
var creditOperation = _.extend(baseOperation, {
    query: {
        'method': 'baz'
    }
});
console.log(url.format(creditOperation));

我想要得到这个creditOperation

{
    host: 'gateway.skebby.it',
    pathname: 'api/send/smseasy/advanced/http.php',
    protocol: 'https',
    query: {
        'username': 'foo',
        'password': 'bar',
        'method': 'baz'
    }
}

0