zoukankan      html  css  js  c++  java
  • Object.assing() 在ie中的兼容问题

    如下图,使用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);
  • 相关阅读:
    #背包#nssl 1488 上升子序列
    #环#nssl 1487 图
    #分治#JZOJ 4211 送你一颗圣诞树
    #概率,dp#JZOJ 4212 我想大声告诉你
    #并查集#JZOJ 4223 旅游
    #dp#nssl 1478 题
    #对顶堆#nssl 1477 赛
    #线段树,离散#nssl 1476 联
    #折半搜索,状压dp#nssl 1471 Y
    #并查集,线性筛#nssl 1470 X
  • 原文地址:https://www.cnblogs.com/javascript9527/p/13470252.html
Copyright © 2011-2022 走看看