javascript - 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 c

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

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};
Share Improve this question asked Sep 5, 2016 at 8:49 duskdeepduskdeep 452 silver badges4 bronze badges 1
  • 1 it is not supported developer.mozilla/it/docs/Web/JavaScript/Reference/… you could use the suggested polyfill – invernomuto Commented Sep 5, 2016 at 8:53
Add a ment  | 

1 Answer 1

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744767329a4592541.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信