Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object doesn't support property or method 'assign'

I setup webpack + babel config

webpack.config.js

...
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
...

.babelrc

{
  "plugins": ["lodash", "transform-object-rest-spread"],
  "presets": [
    ["env", {
      "targets": [
        "> 4%",
        "ie 11",
        "safari 8"
      ]
    }],
    "react",
    "react-optimize"
  ],
  "env": {
    "test": {
      "presets": ["es2015", "react"]
    }
  }
}

In google chrome everything is ok, but in IE 11i have an error

Object doesn't support property or method 'assign'

like image 855
Pavel Avatar asked May 06 '17 18:05

Pavel


Video Answer


1 Answers

You would need to add the object assign transform as well Check https://babeljs.io/docs/plugins/transform-object-assign/

Also notice that it is best practice to include a polyfill instead. The MDN usually gives a polyfill code for ES2015 features. Check https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

For the record, it is:

if (typeof Object.assign != 'function') {
  Object.assign = function(target, varArgs) { // .length of function is 2
    'use strict';
    if (target == null) { // TypeError if undefined or null
      throw new TypeError('Cannot convert undefined or null to object');
    }

    var to = Object(target);

    for (var index = 1; index < arguments.length; index++) {
      var nextSource = arguments[index];

      if (nextSource != null) { // Skip over if undefined or null
        for (var nextKey in nextSource) {
          // Avoid bugs when hasOwnProperty is shadowed
          if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
            to[nextKey] = nextSource[nextKey];
          }
        }
      }
    }
    return to;
  };
}

You want to include this code in your app for browsers that do not support Object.assign. The Babel transform plugin referred above also recommends this approach when building an app and not a library.

like image 112
aldo.roman.nurena Avatar answered Oct 19 '22 21:10

aldo.roman.nurena