The "assign" method is not supported in IE, what to do

I have a small / javascript, Babel script that works fine in Chrome and Firefox, but it doesn't work in Internet Explorer 11.

I hope someone can help me.

Here is my function:

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

Error:

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

This line has an error: var eventData = Object.assign({}, eventItem);

Can I rewrite this line ..?

How to fix it?

+10
source share
3 answers

IE does not support Object.assign ()

Use polyfile

  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 use babel

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

using .babelrc

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

you can find other methods here

+10
source

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

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

The vague dinesh answer is outdated as part of the babel goes. Here's how it was done today.

At the command line:

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

In your .babelrc :

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

A source

0
source

Source: https://habr.com/ru/post/1268328/


All Articles