电脑版微信内置浏览器不支持 Object.assign 方法报错的解决

一H5页面JS里用Object.assign,在移动端跑的好好的,在pc版微信网页浏览器里死活报错,原来是pc微信浏览器内核版本过低问题,找到了解决办法,上代码

//    解决微信浏览器不支持Object.assign这个函数
    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) { // TypeError if undefined or null
                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) { // Skip over if undefined or null
                  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
          });
        }

 

保存,运行,完美!

转载于:https://www.cnblogs.com/tindy/p/9299860.html

你可能感兴趣的:(电脑版微信内置浏览器不支持 Object.assign 方法报错的解决)