欢迎光临
我们一直在努力

微信PC客户端不支持object.assign方法

Recently, I encountered a bug: my frontend project can work normally in WeChat mobile clients but when visited in WeChat PC client for window OS, the page turned out be be blank -_-. After some attempts, I found that it’s due to the unavailability of Object.assign method in WeChat PC client for window OS.

Therefore, we need to write polyfill for Object.assign method. There is not need to think how to write this polyfill ourselves. Just copy from MDN ^_^.

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
  });
}

The code is from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Yakima Teng,

August 28, 2017

In Shanghai, China

赞(0) 打赏(金额可任意指定)
未经允许不得转载:峰间的云 » 微信PC客户端不支持object.assign方法
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

上海宝山顾村棉被加工店 专业加工新旧棉被

查看详情了解一下

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏