首页 > 其他 > 详细

Object.assing() 在ie中的兼容问题

时间:2020-08-10 17:18:56      阅读:71      评论:0      收藏:0      [点我收藏+]

如下图,使用Object.assign() 在ie 中运行是报错的,因为Object.assign是es6新出的语法,ie 中不支持。。。但是在谷歌中是可以正常运行的(处理ie基本都可以、。。)

 

技术分享图片

 

 

在ie中如果使用此方法需要进行能力检测,针对ie浏览器进行特殊处理,解决方案如下,,

技术分享图片

 

 

 

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
if (typeof Object.assign !== ‘function‘) {
  // Must be writable: true, enumerable: false, configurable: true
  Object.defineProperty(Object, "assign", {
    value: function assign(target, varArgs) { // .length of function is 2
      ‘use strict‘;
      if (target === null || target === undefined) {
        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 && nextSource !== undefined) { 
          for (var nextKey in nextSource) {
            // Avoid bugs when hasOwnProperty is shadowed
            if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
              to[nextKey] = nextSource[nextKey];
            }
          }
        }
      }
      return to;
    },
    writable: true,
    configurable: true
  });
}
const returnedTarget = Object.assign(target, source);

console.log(target);

 

Object.assing() 在ie中的兼容问题

原文:https://www.cnblogs.com/javascript9527/p/13470252.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!