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);
  • 相关阅读:
    linux学习之uniq
    hive学习05 参数设置
    【python】调用sm.ms图床api接口,实现上传图片并返回url
    【python】列表与数组之间的相互转换
    更新yum源
    要把RAID5创建卷组
    named-checkconf -z /etc/named.conf
    function_exists
    trigger_error
    命名空间
  • 原文地址:https://www.cnblogs.com/javascript9527/p/13470252.html
Copyright © 2011-2022 走看看