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);
  • 相关阅读:
    Ubuntu使用PBIS认证
    命令行运行Python脚本时传入参数的三种方式
    Python中调用shell
    Install chocolatey
    Configure vyatta
    Linux: Block Port With IPtables
    转:PHPStorm+XDebug进行调试图文教程
    转 Fira Code | 为写程序而生的字体
    转 [PHP]
    转:Drupal 如何得到字段的值?
  • 原文地址:https://www.cnblogs.com/javascript9527/p/13470252.html
Copyright © 2011-2022 走看看