• 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);
  • 相关阅读:
    操作系统简介
    计算机硬件知识整理
    使用 Docker LNMP 部署 PHP 运行环境
    Chart.js 动态图表的使用
    手把手教你使用 GitBook
    手把手教你发布自己的 Composer 包
    PHP 基础篇
    macOS 上安装 PECL
    Yii2 教程
    PHP 基础篇
  • 原文地址:https://www.cnblogs.com/javascript9527/p/13470252.html
Copyright © 2020-2023  润新知