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

  1. // 解决微信浏览器不支持Object.assign这个函数
  2. if (typeof Object.assign != \'function\') {
  3. // Must be writable: true, enumerable: false, configurable: true
  4. Object.defineProperty(Object, "assign", {
  5. value: function assign(target, varArgs) { // .length of function is 2
  6. \'use strict\';
  7. if (target == null) { // TypeError if undefined or null
  8. throw new TypeError(\'Cannot convert undefined or null to object\');
  9. }
  10. var to = Object(target);
  11. for (var index = 1; index < arguments.length; index++) {
  12. var nextSource = arguments[index];
  13. if (nextSource != null) { // Skip over if undefined or null
  14. for (var nextKey in nextSource) {
  15. // Avoid bugs when hasOwnProperty is shadowed
  16. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  17. to[nextKey] = nextSource[nextKey];
  18. }
  19. }
  20. }
  21. }
  22. return to;
  23. },
  24. writable: true,
  25. configurable: true
  26. });
  27. }

 

保存,运行,完美!

版权声明:本文为tindy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tindy/p/9299860.html