IE中不支持方法'assign',该怎么做

时间:2017-05-29 14:06:40

标签: javascript jquery internet-explorer babeljs babel

我有一个小/ javascript,Babel脚本,在Chrome和Firefox浏览器中运行良好,但在Internet Explorer 11中失败。

我希望有人可以帮助我。

这是我的功能:

getDaysWithEvents() {
    // Get all the days in this months calendar view
    // Sibling Months included
    const days = this.getCalendarDays();

    // Set Range Limits on calendar
    this.calendar.setStartDate(days[0]);
    this.calendar.setEndDate(days[days.length - 1]);

    // Iterate over each of the supplied events
    this.props.events.forEach((eventItem) => {

        const eventStart = this.getCalendarDayObject(eventItem.start);
        const eventEnd = this.getCalendarDayObject(eventItem.end);
        const eventMeta = this.getEventMeta(days, eventStart, eventEnd);

        if (eventMeta.isVisibleInView) {
            const eventLength = eventMeta.visibleEventLength;
            const eventSlotIndex = days[eventMeta.firstVisibleDayIndex].eventSlots.indexOf(false);
            let dayIndex = 0;

            // For each day in the event
            while (dayIndex < eventLength) {
                // Clone the event object so we acn add day specfic data
                const eventData = Object.assign({}, eventItem);

                if (dayIndex === 0) {
                     // Flag first day of event
                    eventData.isFirstDay = true;
                }

                if (dayIndex === eventLength - 1) {
                    // Flag last day of event
                    eventData.isLastDay = true;
                }

                if (!eventData.isFirstDay || !eventData.isLastDay) {
                    // Flag between day of event
                    eventData.isBetweenDay = true;
                }

                // Apply Event Data to the correct slot for that day

                days[eventMeta.firstVisibleDayIndex + dayIndex].eventSlots[eventSlotIndex] = eventData;

                // Move to next day of event
                dayIndex++;
            }
        }
    });

    return days;
}

错误:

SCRIPT438: The object does not support the property or method 'assign'

此行有错误:var eventData = Object.assign({}, eventItem);

我可以重写这一行..?

如何解决?

3 个答案:

答案 0 :(得分:10)

IE不支持Object.assign()

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

如果你正在使用babel

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

使用.babelrc

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

您可以找到其他方法here

答案 1 :(得分:6)

如果您使用 jquery ,则可以尝试jQuery.extend( target [, object1 ] [, objectN ] )。你的代码:

const eventData = $.extend({}, eventItem);

答案 2 :(得分:0)

babel部分而言,Dinesh undefined的答案已过时。今天就是这样。

在命令行上:

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

在您的.babelrc中:

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

Source