Native js object merge object copy Object.assign() method es5, es6 implementation

Implementation idea: For browsers that support es6, you can directly use Object.assign() to merge objects. For browsers that only support es5, we use the pollyfill method.

effect

The Object.assign() method is used to copy the enumerable property values ​​of one or more source objects to the target object, and the return value is the target object.

grammar

Object.assign(target, ...sources)

parameter

target : target object
sources : source object

return value

target

describe

The Object.assign method copies only the enumerable properties in the source object and the properties of the object itself. It uses [[Get]] on the source object and [[Set]] on the target object, which calls  getters  and  setters . It is not suitable for incorporating a getter-containing object property into a prototype. To copy property definitions along with enumerability into a prototype, the  Object.getOwnPropertyDescriptor()  and  Object.defineProperty()  methods should be used.

Properties of type String  and  Symbol  are copied.

When an error occurs, such as a property that is not writable, a  TypeError will be thrown  and the target object will remain unchanged.

Note that  Object.assign()  will not report an error when the source object is  null  or  undefined  .

jian example

clone object

var obj = {a: 1};
var copy = Object.assign({}, obj);
console.log(copy); // {a: 1};

Merge objects

var o1 = {a: 1};
var o2 = {b: 2};
var o3 = {c: 3};

var obj = Object.assign(o1, o2, o3);
console.log(obj); //{a: 1, b: 2, c: 3}
console.log(o1); //{a: 1, b: 2, c: 3}, 目标对象被改变了

ES5 version implementation method

/**
     * 对象合并polyfill
     * */
    function zyEs6AssignPolyfill() {
        if (!Object.assign) {
            Object.defineProperty(Object, "assign", {
                enumerable: false,
                configurable: true,
                writable: true,
                value: function (target, firstSource) {
                    "use strict";
                    if (target === undefined || target === null)
                        throw new TypeError("Cannot convert first argument to object");
                    var to = Object(target);
                    for (var i = 1; i < arguments.length; i++) {
                        var nextSource = arguments[i];
                        if (nextSource === undefined || nextSource === null) continue;
                        var keysArray = Object.keys(Object(nextSource));
                        for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
                            var nextKey = keysArray [nextIndex];
                            var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
                            if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey];
                        }
                    }
                    return to;
                }
            });
        }
    }
References: http://www.cnblogs.com/heiye168/p/5689006.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774472&siteId=291194637