简体   繁体   English

在JavaScript中实现Object.Assign()的另一种方法

[英]Another way to achieve Object.Assign() in JavaScript

Object.assign(home, action.home.fields);

I'm assigning action.home.fields to my home object. 我正在将action.home.fields分配给我的home对象。 Found out that the Object.assign isn't supported in IE. 发现IE中不支持Object.assign

What's the equivalent to what I'm doing that's support in IE? 什么相当于我在IE中的支持?

I read the spread operator ? 我读了点spread operator So would: 那会:

home = {...home, ...action.home.fields};

work? 工作?

I did check the support on that, wasn't support massively either. 我确实检查了支持,也没有大量支持。

This is the not-so-official polyfill for Object.assign() : 这是Object.assign()的不那么官方的polyfill:

if (typeof Object.assign != 'function') {
  (function () {
    Object.assign = function (target) {
      'use strict';
      // We must check against these specific cases.
      if (target === undefined || target === null) {
        throw new TypeError('Cannot convert undefined or null to object');
      }

      var output = Object(target);
      for (var index = 1; index < arguments.length; index++) {
        var source = arguments[index];
        if (source !== undefined && source !== null) {
          for (var nextKey in source) {
            if (source.hasOwnProperty(nextKey)) {
              output[nextKey] = source[nextKey];
            }
          }
        }
      }
      return output;
    };
  })();
}

Taken from MDN . 取自MDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM