method 'assign' not supported in IE, what to do

后端 未结 3 834
遇见更好的自我
遇见更好的自我 2021-01-02 00:59

I have a small /javascript,Babel script, that runs just fine in Chrome and Firefox browsers, but it fails in Internet Explorer 11.

I hope somebody can help me.

相关标签:
3条回答
  • 2021-01-02 01:25

    Dinesh undefined's answer is outdated as far as the babel part goes. This is how it's done today.

    On the commandline:

    npm install --save-dev @babel/plugin-transform-object-assign

    In your .babelrc:

    { "plugins": ["@babel/plugin-transform-object-assign"] }

    Source

    0 讨论(0)
  • 2021-01-02 01:44

    If you are using jquery, you can try jQuery.extend( target [, object1 ] [, objectN ] ). Your code:

    const eventData = $.extend({}, eventItem);
    
    0 讨论(0)
  • 2021-01-02 01:48

    IE doesn't support Object.assign()

    Use polyfil

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

    If you are using babel

    npm install --save-dev babel-plugin-transform-object-assign
    

    using .babelrc

    {
      "plugins": ["transform-object-assign"]
    }
    

    you can find other methods here

    0 讨论(0)
提交回复
热议问题