js 深复制对象

/**
         * 深复制对象
         */
        (function () {
            //深复制对象方法
            var cloneObj = function (obj) {
                var newO = {};

                if (obj instanceof Array) {
                    newO = [];
                }
                for (var key in obj) {
                    var val = obj[key];
                    newO[key] = typeof val === 'object' ? arguments.callee(val) : val;
                }
                return newO;
            };

            //测试
            var a = {a:{a:123}},
                    b = cloneObj(a);

            b.a.a = 456;
            alert(a.a.a);
        })();

你可能感兴趣的:(js 深复制对象)