duskdeep duskdeep - 3 months ago 18
jQuery Question

Safari TypeError: 'undefined' is not a function (evaluating 'Object.assign(...)')

I would like to assign some object to a target object by Javascript Object.assign(). It works fine in chrome. In safari though I get an error.

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


Object

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

Answer

It's not supported by safari, but you can always create a 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;
    }
  });
}

Or start using a transpiler like Babel

Reference for polyfill