Safari TypeError:'undefined'不是函数(评估'Object.assign(...)')

时间:2016-09-05 08:49:54

标签: javascript jquery object key-value

我想通过Javascript Object.assign()将一些对象分配给目标对象。它在chrome中工作正常。在safari中虽然我收到了错误。

TypeError: 'undefined' is not a function (evaluating 'Object.assign(wkObj,tObj,list)')

对象

    var list = {'width':'100px'};
    var tz = "translateZ(-18px)";
    var tObj = {'transform': tz};
    var wkObj = {'-webkit-transform': tz};

1 个答案:

答案 0 :(得分:5)

safari不支持它,但你总是可以创建一个polyfill:

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;
    }
  });
}

或者开始使用像Babel

这样的转录程序

Reference for polyfill