如何深层复制javascript中的对象作为对象而不是数组

far*_*oft 2 javascript jquery deep-copy

如何深层复制javascript中的对象作为对象而不是数组

var x = {attr1: "value", attr2: "value"};
var y = $.extend(true, [], x);
var z = $.extend(true, [], x);

alert($.type(x)); // object
alert($.type(y)); // array [ why not object too ]
alert($.type(z)); // array [ why not object too ]
Run Code Online (Sandbox Code Playgroud)

Sus*_* -- 5

代替

var y = $.extend(true, [], x);
Run Code Online (Sandbox Code Playgroud)

尝试

var y = $.extend(true, {}, x);
                       ^^--------- Empty object instead of empty Array
Run Code Online (Sandbox Code Playgroud)