原生js合并两个json方法兼容IE浏览器
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>前端哥——原生js合并两个json方法兼容IE浏览器</title> </head> <body> <script> // IE 兼容方法 if (typeof Object.assign != 'function') { Object.assign = function(target) { 'use strict'; if (target == null) { throw new TypeError('Cannot convert undefined or null to object'); } target = Object(target); for (var index = 1; index < arguments.length; index ) { var source = arguments[index]; if (source != null) { for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } } return target; }; } // 以下为测试代码 var target = { a: 1, b: 2, c:5 }; var source = { b: 4, c: 12121, key:"news" }; var returnedTarget = Object.assign(target, source); console.log(returnedTarget); alert(JSON.stringify(returnedTarget)) </script> </body></html>