IE浏览器兼容Object.assign踩过的坑
1、背景:
使用Object.assign
,又在IE浏览器上访问时,会报该方法不存在~
ERROR:对象不支持“assign”属性或方法
错误说IE上Object对象上不存在assign方法,查一下兼容性
2、解决方案:
在JavaScript初始化时添加以下方法:
网址: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
js代码:
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
});
}
觉得可以,记得右上角点赞哦!如有不正确的地方,欢迎各位大神指教!
【The End】
附言:
[抽空为大家整理了一些文章]
【表格】html大于号转义符&;gt;—小于号转义符&;lt;
div+CSS实现段落首行缩进两个字符(text-indent标签)
鼠标mouse事件冒泡处理:onmouseover 、onmouseout 与onmouseenter 、onmouseleave的区别
Mysql启动错误:“InnoDB: Error: unable to create temporary file”的解决方法