// 2.Object.assign 对象的合并---也可用于对象的浅拷贝 let config1 = { host: "localhost", port: 3306, name: "root", password: "root", test1: "测试1", };
let config2 = { host: "https://www.baidu.com", port: 33060, name: "百度", password: 123, test2: "测试2", }; console.log(Object.assign(config1, config2)); //后面对象里的属性会把前面对象里同名的属性覆盖掉,不相同的则保留
// 3.Object.setPrototypeOf,Object.getPrototypeOf let school = { name: "xxx", };
let cities = { address: ["xx1", "xx2", "xx3"], };
// 此方法不建议这样做,应该在创建对象的时候就把原型设置好,效率高 Object.create(proto, [propertiesObject]) Object.setPrototypeOf(school, cities); //为school设置原型对象为cities console.log(school); console.log(Object.getPrototypeOf(school)); </script> </body> </html>